Additional Approach Changes
This commit is contained in:
parent
da0beaeee0
commit
50515d3070
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.list.multiple.objecttypes;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class AlternativeMultipeTypeList {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// List of Numbers
|
||||
ArrayList<Number> myList = new ArrayList<>();
|
||||
myList.add(1.2);
|
||||
myList.add(2);
|
||||
myList.add(-3.5);
|
||||
|
||||
// List of Map
|
||||
ArrayList<Map> diffMapList = new ArrayList<>();
|
||||
diffMapList.add(new HashMap<>());
|
||||
diffMapList.add(new TreeMap<>());
|
||||
diffMapList.add(new LinkedHashMap<>());
|
||||
|
||||
ArrayList<CustomObject> objList = new ArrayList<>();
|
||||
objList.add(new CustomObject("obj1", 1));
|
||||
objList.add(new CustomObject("obj2", 2));
|
||||
|
||||
List<Object> dataList = new ArrayList<>();
|
||||
|
||||
Predicate<Object> myPricate = inputData -> (inputData instanceof String || inputData instanceof Integer);
|
||||
|
||||
UserFunctionalInterface myInterface = (listObj, data) -> {
|
||||
if (myPricate.test(data))
|
||||
listObj.add(data);
|
||||
return listObj;
|
||||
};
|
||||
|
||||
myInterface.addToList(dataList, Integer.valueOf(2));
|
||||
myInterface.addToList(dataList, Double.valueOf(3.33));
|
||||
myInterface.addToList(dataList, "String Value");
|
||||
myInterface.printList(dataList);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,12 +2,22 @@ package com.baeldung.list.multiple.objecttypes;
|
|||
|
||||
public class CustomObject {
|
||||
String classData;
|
||||
Integer intData;
|
||||
|
||||
CustomObject(String classData) {
|
||||
this.classData = classData;
|
||||
}
|
||||
|
||||
CustomObject(String classData, Integer intData) {
|
||||
this.classData = classData;
|
||||
this.intData = intData;
|
||||
}
|
||||
|
||||
public String getClassData() {
|
||||
return this.classData;
|
||||
}
|
||||
|
||||
public Integer getIntData() {
|
||||
return this.intData;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.list.multiple.objecttypes;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface UserFunctionalInterface {
|
||||
|
||||
List<Object> addToList(List<Object> list, Object data);
|
||||
|
||||
default void printList(List<Object> dataList) {
|
||||
for (Object data : dataList) {
|
||||
if (data instanceof String stringData)
|
||||
System.out.println("String Data: " + stringData);
|
||||
if (data instanceof Integer intData)
|
||||
System.out.println("Integer Data: " + intData);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue