Merge branch 'eugenp:master' into master
This commit is contained in:
@@ -5,3 +5,4 @@
|
||||
- [Collecting Stream Elements into a List in Java](https://www.baeldung.com/java-stream-to-list-collecting)
|
||||
- [New Features in Java 16](https://www.baeldung.com/java-16-new-features)
|
||||
- [Guide to Java 8 groupingBy Collector](https://www.baeldung.com/java-groupingby-collector)
|
||||
- [Value-Based Classes in Java](https://www.baeldung.com/java-value-based-classes)
|
||||
|
||||
@@ -30,6 +30,11 @@ public final class Point {
|
||||
return new Point(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Point{" + "x=" + x + ", y=" + y + ", z=" + z + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other == null || getClass() != other.getClass())
|
||||
|
||||
+6
-3
@@ -1,14 +1,17 @@
|
||||
package com.baeldung.value_based_class;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ValueBasedClassUnitTest {
|
||||
@Test
|
||||
public void givenAutoboxedAndPrimitive_whenCompared_thenReturnEquals() {
|
||||
int primitive_a = 125;
|
||||
Integer obj_a = 125; // this is autoboxed
|
||||
Assert.assertSame(primitive_a, obj_a);
|
||||
List<Integer> list = new ArrayList<>();
|
||||
list.add(1); // this is autoboxed
|
||||
Assert.assertEquals(list.get(0), Integer.valueOf(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-18</artifactId>
|
||||
<name>core-java-18</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<release>${maven.compiler.release}</release>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<forkCount>1</forkCount>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.surefire</groupId>
|
||||
<artifactId>surefire-api</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>18</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>18</maven.compiler.target.version>
|
||||
<maven.compiler.release>18</maven.compiler.release>
|
||||
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FinalizationExamples {
|
||||
FileInputStream fis = null;
|
||||
|
||||
public void readFileOperationWithFinalization() throws IOException {
|
||||
try {
|
||||
fis = new FileInputStream("input.txt");
|
||||
// perform operation on the file
|
||||
System.out.println(fis.readAllBytes().length);
|
||||
|
||||
} finally {
|
||||
if (fis != null)
|
||||
fis.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void readFileOperationWithTryWith() throws IOException {
|
||||
try (FileInputStream fis = new FileInputStream("input.txt")) {
|
||||
// perform operations
|
||||
System.out.println(fis.readAllBytes().length);
|
||||
}
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import java.lang.ref.Cleaner;
|
||||
|
||||
public class MyCleanerResourceClass implements AutoCloseable {
|
||||
private static Resource resource;
|
||||
|
||||
private static final Cleaner cleaner = Cleaner.create();
|
||||
private final Cleaner.Cleanable cleanable;
|
||||
|
||||
public MyCleanerResourceClass() {
|
||||
resource = new Resource();
|
||||
this.cleanable = cleaner.register(this, new CleaningState());
|
||||
}
|
||||
|
||||
public void useResource() {
|
||||
// using the resource here
|
||||
resource.use();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
// perform actions to close all underlying resources
|
||||
this.cleanable.clean();
|
||||
}
|
||||
|
||||
static class CleaningState implements Runnable {
|
||||
CleaningState() {
|
||||
// constructor
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// some cleanup action
|
||||
System.out.println("Cleanup done");
|
||||
}
|
||||
}
|
||||
|
||||
static class Resource {
|
||||
void use() {
|
||||
System.out.println("Using the resource");
|
||||
}
|
||||
|
||||
void close() {
|
||||
System.out.println("Cleanup done");
|
||||
}
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MyCloseableResourceClass implements AutoCloseable {
|
||||
|
||||
private final FileInputStream fis;
|
||||
|
||||
public MyCloseableResourceClass() throws FileNotFoundException {
|
||||
this.fis = new FileInputStream("src/main/resources/file.txt");
|
||||
|
||||
}
|
||||
|
||||
public int getByteLength() throws IOException {
|
||||
System.out.println("Some operation");
|
||||
return this.fis.readAllBytes().length;
|
||||
}
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
System.out.println("Finalized object");
|
||||
this.fis.close();
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MyFinalizableResourceClass {
|
||||
private FileInputStream fis;
|
||||
|
||||
public MyFinalizableResourceClass() throws FileNotFoundException {
|
||||
this.fis = new FileInputStream("src/main/resources/file.txt");
|
||||
}
|
||||
|
||||
public int getByteLength() throws IOException {
|
||||
System.out.println("Some operation");
|
||||
return this.fis.readAllBytes().length;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
System.out.println("Finalized object");
|
||||
this.fis.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
This is a test file.
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FinalizationCloseableCleanerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenMyFinalizationResource_whenUsingFinalize_thenShouldClean() {
|
||||
assertDoesNotThrow(() -> {
|
||||
MyFinalizableResourceClass mfr = new MyFinalizableResourceClass();
|
||||
mfr.getByteLength();
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void givenMyCleanerResource_whenUsingCleanerAPI_thenShouldClean() {
|
||||
assertDoesNotThrow(() -> {
|
||||
try (MyCleanerResourceClass myCleanerResourceClass = new MyCleanerResourceClass()) {
|
||||
myCleanerResourceClass.useResource();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCloseableResource_whenUsingTryWith_thenShouldClose() throws IOException {
|
||||
int length = 0;
|
||||
try (MyCloseableResourceClass mcr = new MyCloseableResourceClass()) {
|
||||
length = mcr.getByteLength();
|
||||
}
|
||||
Assert.assertEquals(20, length);
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,5 @@
|
||||
## Relevant Articles
|
||||
- [Sequenced Collections in Java 21](https://www.baeldung.com/java-21-sequenced-collections)
|
||||
- [Sequenced Collections in Java 21](https://www.baeldung.com/java-21-sequenced-collections)
|
||||
- [String Templates in Java 21](https://www.baeldung.com/java-21-string-templates)
|
||||
- [Unnamed Classes and Instance Main Methods in Java 21](https://www.baeldung.com/java-21-unnamed-class-instance-main)
|
||||
- [Unnamed Patterns and Variables in Java 21](https://www.baeldung.com/java-unnamed-patterns-variables)
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
void main() {
|
||||
System.out.println("Hello, World!");
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.unnamedclasses;
|
||||
|
||||
public class HelloWorldChild extends HelloWorldSuper {
|
||||
void main() {
|
||||
System.out.println("Hello, World!");
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.unnamedclasses;
|
||||
|
||||
public class HelloWorldSuper {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello from the superclass");
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
private String getMessage() {
|
||||
return "Hello, World!";
|
||||
}
|
||||
void main() {
|
||||
System.out.println(getMessage());
|
||||
}
|
||||
@@ -4,3 +4,4 @@ This module contains articles about Java 9 streams
|
||||
|
||||
### Relevant Articles:
|
||||
- [How to Break from Java Stream forEach](https://www.baeldung.com/java-break-stream-foreach)
|
||||
- [Creating Stream of Regex Matches](https://www.baeldung.com/java-stream-regex-matches)
|
||||
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.array.conversions;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class CharArrayToIntArrayUtils {
|
||||
|
||||
static int[] usingGetNumericValueMethod(char[] chars) {
|
||||
if (chars == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int[] ints = new int[chars.length];
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
ints[i] = Character.getNumericValue(chars[i]);
|
||||
}
|
||||
|
||||
return ints;
|
||||
}
|
||||
|
||||
static int[] usingDigitMethod(char[] chars) {
|
||||
if (chars == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int[] ints = new int[chars.length];
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
ints[i] = Character.digit(chars[i], 10);
|
||||
}
|
||||
|
||||
return ints;
|
||||
}
|
||||
|
||||
static int[] usingStreamApiMethod(char[] chars) {
|
||||
if (chars == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new String(chars).chars()
|
||||
.map(c -> c - 48)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
static int[] usingParseIntMethod(char[] chars) {
|
||||
if (chars == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int[] ints = new int[chars.length];
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
ints[i] = Integer.parseInt(String.valueOf(chars[i]));
|
||||
}
|
||||
|
||||
return ints;
|
||||
}
|
||||
|
||||
static int[] usingArraysSetAllMethod(char[] chars) {
|
||||
if (chars == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int[] ints = new int[chars.length];
|
||||
Arrays.setAll(ints, i -> Character.getNumericValue(chars[i]));
|
||||
|
||||
return ints;
|
||||
}
|
||||
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.array.conversions;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CharArrayToIntArrayUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenCharArray_whenUsingGetNumericValueMethod_shouldGetIntArray() {
|
||||
int[] expected = { 2, 3, 4, 5 };
|
||||
char[] chars = { '2', '3', '4', '5' };
|
||||
int[] result = CharArrayToIntArrayUtils.usingGetNumericValueMethod(chars);
|
||||
|
||||
assertArrayEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCharArray_whenUsingDigitMethod_shouldGetIntArray() {
|
||||
int[] expected = { 1, 2, 3, 6 };
|
||||
char[] chars = { '1', '2', '3', '6' };
|
||||
int[] result = CharArrayToIntArrayUtils.usingDigitMethod(chars);
|
||||
|
||||
assertArrayEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCharArray_whenUsingStreamApi_shouldGetIntArray() {
|
||||
int[] expected = { 9, 8, 7, 6 };
|
||||
char[] chars = { '9', '8', '7', '6' };
|
||||
int[] result = CharArrayToIntArrayUtils.usingStreamApiMethod(chars);
|
||||
|
||||
assertArrayEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCharArray_whenUsingParseIntMethod_shouldGetIntArray() {
|
||||
int[] expected = { 9, 8, 7, 6 };
|
||||
char[] chars = { '9', '8', '7', '6' };
|
||||
int[] result = CharArrayToIntArrayUtils.usingParseIntMethod(chars);
|
||||
|
||||
assertArrayEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCharArray_whenUsingArraysSetAllMethod_shouldGetIntArray() {
|
||||
int[] expected = { 4, 9, 2, 3 };
|
||||
char[] chars = { '4', '9', '2', '3' };
|
||||
int[] result = CharArrayToIntArrayUtils.usingArraysSetAllMethod(chars);
|
||||
|
||||
assertArrayEquals(expected, result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,3 +9,4 @@ This module contains complete guides about arrays in Java
|
||||
- [Guide to ArrayStoreException](https://www.baeldung.com/java-arraystoreexception)
|
||||
- [Creating a Generic Array in Java](https://www.baeldung.com/java-generic-array)
|
||||
- [Maximum Size of Java Arrays](https://www.baeldung.com/java-arrays-max-size)
|
||||
- [Merge Two Arrays and Remove Duplicates in Java](https://www.baeldung.com/java-merge-two-arrays-delete-duplicates)
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
## Relevant Articles
|
||||
- [Find the Middle Element of an Array in Java](https://www.baeldung.com/java-array-middle-item)
|
||||
@@ -14,4 +14,3 @@ This module contains articles about advanced operations on arrays in Java. They
|
||||
- [Slicing Arrays in Java](https://www.baeldung.com/java-slicing-arrays)
|
||||
- [Combining Two or More Byte Arrays](https://www.baeldung.com/java-concatenate-byte-arrays)
|
||||
- [Calculating the Sum of Two Arrays in Java](https://www.baeldung.com/java-sum-arrays-element-wise)
|
||||
- [Find the Middle Element of an Array in Java](https://www.baeldung.com/java-array-middle-item)
|
||||
|
||||
@@ -5,3 +5,4 @@ This module contains articles about Java Character Class
|
||||
### Relevant Articles:
|
||||
- [Character#isAlphabetic vs. Character#isLetter](https://www.baeldung.com/java-character-isletter-isalphabetic)
|
||||
- [Difference Between Java’s “char” and “String”](https://www.baeldung.com/java-char-vs-string)
|
||||
- [Increment Character in Java](https://www.baeldung.com/java-char-sequence)
|
||||
|
||||
@@ -5,4 +5,6 @@
|
||||
### Relevant Articles:
|
||||
- [Introduction to Roaring Bitmap](https://www.baeldung.com/java-roaring-bitmap-intro)
|
||||
- [Creating Custom Iterator in Java](https://www.baeldung.com/java-creating-custom-iterator)
|
||||
- [Difference Between Arrays.sort() and Collections.sort()](https://www.baeldung.com/java-arrays-collections-sort-methods)
|
||||
- [Skipping the First Iteration in Java](https://www.baeldung.com/java-skip-first-iteration)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-4)
|
||||
|
||||
@@ -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/java-arraylist-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>
|
||||
+51
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+22
@@ -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;
|
||||
}
|
||||
}
|
||||
+40
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user