Merge pull request #14770 from niket17590/feature/BAEL-6963-ArrayList
BAEL-6963 - ArrayList with Multiple Object Types
This commit is contained in:
commit
d1aa92f178
|
@ -0,0 +1,6 @@
|
|||
## Core Java Collections ArrayList
|
||||
|
||||
This module contains articles about the Java ArrayList collection
|
||||
|
||||
### Relevant Articles:
|
||||
- [Create an ArrayList with Multiple Object Types](https://www.baeldung.com/arraylist-with-multiple-object-types)
|
|
@ -0,0 +1,32 @@
|
|||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-collections-array-list-2</artifactId>
|
||||
<name>core-java-collections-array-list-2</name>
|
||||
<packaging>jar</packaging>
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${maven-compiler-plugin.source}</source>
|
||||
<target>${maven-compiler-plugin.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven-compiler-plugin.source>17</maven-compiler-plugin.source>
|
||||
<maven-compiler-plugin.target>17</maven-compiler-plugin.target>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.list.multipleobjecttypes;
|
||||
|
||||
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 Parent Class
|
||||
ArrayList<Number> myList = new ArrayList<>();
|
||||
myList.add(1.2);
|
||||
myList.add(2);
|
||||
myList.add(-3.5);
|
||||
|
||||
// List of Interface type
|
||||
ArrayList<Map> diffMapList = new ArrayList<>();
|
||||
diffMapList.add(new HashMap<>());
|
||||
diffMapList.add(new TreeMap<>());
|
||||
diffMapList.add(new LinkedHashMap<>());
|
||||
|
||||
// List of Custom Object
|
||||
ArrayList<CustomObject> objList = new ArrayList<>();
|
||||
objList.add(new CustomObject("String"));
|
||||
objList.add(new CustomObject(2));
|
||||
|
||||
// List via Functional Interface
|
||||
List<Object> dataList = new ArrayList<>();
|
||||
|
||||
Predicate<Object> myPredicate = inputData -> (inputData instanceof String || inputData instanceof Integer);
|
||||
|
||||
UserFunctionalInterface myInterface = (listObj, data) -> {
|
||||
if (myPredicate.test(data))
|
||||
listObj.add(data);
|
||||
else
|
||||
System.out.println("Skipping input as data not allowed for class: " + data.getClass()
|
||||
.getSimpleName());
|
||||
return listObj;
|
||||
};
|
||||
|
||||
myInterface.addToList(dataList, Integer.valueOf(2));
|
||||
myInterface.addToList(dataList, Double.valueOf(3.33));
|
||||
myInterface.addToList(dataList, "String Value");
|
||||
myInterface.printList(dataList);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.list.multipleobjecttypes;
|
||||
|
||||
public class CustomObject {
|
||||
String classData;
|
||||
Integer intData;
|
||||
|
||||
CustomObject(String classData) {
|
||||
this.classData = classData;
|
||||
}
|
||||
|
||||
CustomObject(Integer intData) {
|
||||
this.intData = intData;
|
||||
}
|
||||
|
||||
public String getClassData() {
|
||||
return this.classData;
|
||||
}
|
||||
|
||||
public Integer getIntData() {
|
||||
return this.intData;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.list.multipleobjecttypes;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class MultipleObjectTypeArrayList {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
ArrayList<Object> multiTypeList = new ArrayList<>();
|
||||
|
||||
multiTypeList.add(Integer.valueOf(10));
|
||||
multiTypeList.add(Double.valueOf(11.5));
|
||||
multiTypeList.add("String Data");
|
||||
multiTypeList.add(Arrays.asList(1, 2, 3));
|
||||
multiTypeList.add(new CustomObject("Class Data"));
|
||||
multiTypeList.add(BigInteger.valueOf(123456789));
|
||||
multiTypeList.add(LocalDate.of(2023, 9, 19));
|
||||
|
||||
for (Object dataObj : multiTypeList) {
|
||||
if (dataObj instanceof Integer intData)
|
||||
System.out.println("Integer Data : " + intData);
|
||||
else if (dataObj instanceof Double doubleData)
|
||||
System.out.println("Double Data : " + doubleData);
|
||||
else if (dataObj instanceof String stringData)
|
||||
System.out.println("String Data : " + stringData);
|
||||
else if (dataObj instanceof List<?> intList)
|
||||
System.out.println("List Data : " + intList);
|
||||
else if (dataObj instanceof CustomObject customObj)
|
||||
System.out.println("CustomObject Data : " + customObj.getClassData());
|
||||
else if (dataObj instanceof BigInteger bigIntData)
|
||||
System.out.println("BigInteger Data : " + bigIntData);
|
||||
else if (dataObj instanceof LocalDate localDate)
|
||||
System.out.println("LocalDate Data : " + localDate.toString());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.list.multipleobjecttypes;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -28,13 +28,14 @@
|
|||
<!--<module>core-java-streams-2</module> -->
|
||||
<!--<module>core-java-sun</module> -->
|
||||
<module>core-java-9-improvements</module>
|
||||
<module>core-java-collections-array-list</module>
|
||||
<module>core-java-9-streams</module>
|
||||
<module>core-java-9</module>
|
||||
<module>core-java-10</module>
|
||||
<module>core-java-11</module>
|
||||
<module>core-java-11-2</module>
|
||||
<module>core-java-11-3</module>
|
||||
<module>core-java-collections-array-list</module>
|
||||
<module>core-java-collections-array-list-2</module>
|
||||
<module>core-java-collections-list-4</module>
|
||||
<module>core-java-collections-list-5</module>
|
||||
<module>core-java-collections-maps-4</module>
|
||||
|
|
Loading…
Reference in New Issue