[JAVA-5730] convert list of objects to list of strings (#13166)
* [JAVA-5730] convert list of objects to list of strings * [JAVA-5730] added new unit tests with null and non-null lists. modified pom.xml with java version to 16 * [JAVA-5730] simplified User class and formatted the codebase * [JAVA-5730] added module in jdk-9 and above profile * [JAVA-5730] removed arraylist module from other profiles of core-java pom * [JAVA-5730] java util opened for reflection surefire plugin Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
This commit is contained in:
parent
dcb6c853ec
commit
fd1c286570
|
@ -5,6 +5,33 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-collections-array-list</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<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>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>
|
||||
--add-opens java.base/java.util=ALL-UNNAMED
|
||||
</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<maven-compiler-plugin.source>16</maven-compiler-plugin.source>
|
||||
<maven-compiler-plugin.target>16</maven-compiler-plugin.target>
|
||||
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
||||
</properties>
|
||||
<name>core-java-collections-array-list</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
@ -20,6 +47,12 @@
|
|||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>31.1-jre</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.listofobjectstolistofstring;
|
||||
|
||||
public class Node {
|
||||
|
||||
private final int x;
|
||||
private final int y;
|
||||
|
||||
public Node(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Node (" + "x=" + x + ", y=" + y + ')';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.listofobjectstolistofstring;
|
||||
|
||||
public class User {
|
||||
private final String fullName;
|
||||
|
||||
public User(String fullName) {
|
||||
this.fullName = fullName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User (" + "full name='" + fullName + ')';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
package com.baeldung.listofobjectstolistofstring;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ConvertObjectListToStringListUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenObjectList_whenForEachUsedToConvert_thenReturnSuccess() {
|
||||
List<String> outputList = new ArrayList<>(objectListWithNull().size());
|
||||
for (Object obj : objectListWithNull()) {
|
||||
outputList.add(Objects.toString(obj, null));
|
||||
}
|
||||
Assert.assertEquals(expectedStringListWithNull(), outputList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectList_whenUsingStreamsToConvert_thenReturnSuccess() {
|
||||
List<String> outputList;
|
||||
outputList = objectListWithNull().stream()
|
||||
.map((obj) -> Objects.toString(obj, null))
|
||||
.collect(Collectors.toList());
|
||||
Assert.assertEquals(expectedStringListWithNull(), outputList);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectList_whenUsingStreamsUnmodifiableListToConvert_thenReturnSuccess() {
|
||||
List<String> outputList;
|
||||
outputList = objectListWithNull().stream()
|
||||
.filter(Objects::nonNull)
|
||||
.map((obj) -> Objects.toString(obj, null))
|
||||
.collect(Collectors.toUnmodifiableList());
|
||||
Assert.assertEquals(expectedStringListWithoutNull(), outputList);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectList_whenUsingGuavaTransform_thenReturnSuccess() {
|
||||
List<String> outputList;
|
||||
outputList = Lists.transform(objectListWithNull(), obj -> Objects.toString(obj, null));
|
||||
Assert.assertEquals(expectedStringListWithNull(), outputList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectListWithNoNull_whenUsingToList_thenReturnSuccess() {
|
||||
List<String> outputList;
|
||||
outputList = objectListWithoutNull().stream()
|
||||
.map((obj) -> Objects.toString(obj, null))
|
||||
.toList();
|
||||
Assert.assertEquals(expectedStringListWithoutNull(), outputList);
|
||||
}
|
||||
|
||||
private List<String> expectedStringListWithNull() {
|
||||
List<String> listOfStrings = new ArrayList<>();
|
||||
listOfStrings.add("1");
|
||||
listOfStrings.add("true");
|
||||
listOfStrings.add("hello");
|
||||
listOfStrings.add(Double.toString(273773.98));
|
||||
listOfStrings.add(null);
|
||||
listOfStrings.add(new Node(2, 4).toString());
|
||||
listOfStrings.add(new User("John Doe").toString());
|
||||
return listOfStrings;
|
||||
}
|
||||
|
||||
private List<Object> objectListWithNull() {
|
||||
List<Object> listOfStrings = new ArrayList<>();
|
||||
listOfStrings.add(1);
|
||||
listOfStrings.add(true);
|
||||
listOfStrings.add("hello");
|
||||
listOfStrings.add(Double.valueOf(273773.98));
|
||||
listOfStrings.add(null);
|
||||
listOfStrings.add(new Node(2, 4));
|
||||
listOfStrings.add(new User("John Doe"));
|
||||
return listOfStrings;
|
||||
}
|
||||
|
||||
private List<String> expectedStringListWithoutNull() {
|
||||
return List.of("1", "true", "hello", Double.toString(273773.98), new Node(2, 4).toString(), new User("John Doe").toString());
|
||||
}
|
||||
|
||||
private List<Object> objectListWithoutNull() {
|
||||
return List.of(1, true, "hello", Double.valueOf(273773.98), new Node(2, 4), new User("John Doe"));
|
||||
}
|
||||
}
|
|
@ -31,7 +31,6 @@
|
|||
<module>core-java-collections-2</module>
|
||||
<module>core-java-collections-3</module>
|
||||
<module>core-java-collections-4</module>
|
||||
<module>core-java-collections-array-list</module>
|
||||
<module>core-java-collections-conversions</module>
|
||||
<module>core-java-collections-conversions-2</module>
|
||||
<module>core-java-collections-set-2</module>
|
||||
|
|
1
pom.xml
1
pom.xml
|
@ -1133,6 +1133,7 @@
|
|||
<!-- <module>core-java-modules/core-java-19</module> --> <!-- uses preview features, to be decided how to handle -->
|
||||
<module>core-java-modules/core-java-collections-set</module>
|
||||
<module>core-java-modules/core-java-collections-list-4</module>
|
||||
<module>core-java-modules/core-java-collections-array-list</module>
|
||||
<module>core-java-modules/core-java-collections-maps-4</module>
|
||||
<module>core-java-modules/core-java-collections-maps-5</module>
|
||||
<module>core-java-modules/core-java-concurrency-simple</module>
|
||||
|
|
Loading…
Reference in New Issue