[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:
Bhaskar Ghosh Dastidar 2023-01-19 08:23:05 +05:30 committed by GitHub
parent dcb6c853ec
commit fd1c286570
6 changed files with 157 additions and 1 deletions

View File

@ -5,6 +5,33 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-array-list</artifactId> <artifactId>core-java-collections-array-list</artifactId>
<version>0.1.0-SNAPSHOT</version> <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> <name>core-java-collections-array-list</name>
<packaging>jar</packaging> <packaging>jar</packaging>
@ -20,6 +47,12 @@
<artifactId>commons-collections4</artifactId> <artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version> <version>${commons-collections4.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -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 + ')';
}
}

View File

@ -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 + ')';
}
}

View File

@ -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"));
}
}

View File

@ -31,7 +31,6 @@
<module>core-java-collections-2</module> <module>core-java-collections-2</module>
<module>core-java-collections-3</module> <module>core-java-collections-3</module>
<module>core-java-collections-4</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</module>
<module>core-java-collections-conversions-2</module> <module>core-java-collections-conversions-2</module>
<module>core-java-collections-set-2</module> <module>core-java-collections-set-2</module>

View File

@ -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-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-set</module>
<module>core-java-modules/core-java-collections-list-4</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-4</module>
<module>core-java-modules/core-java-collections-maps-5</module> <module>core-java-modules/core-java-collections-maps-5</module>
<module>core-java-modules/core-java-concurrency-simple</module> <module>core-java-modules/core-java-concurrency-simple</module>