BAEL-1073 Converting a List to String in Java (#2489)
This commit is contained in:
parent
b6674f68dc
commit
621d0d2c62
@ -0,0 +1,31 @@
|
|||||||
|
package org.baeldung.java.lists;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ListToSTring {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenListToString_thenPrintDefault() {
|
||||||
|
List<Integer> intLIst = Arrays.asList(1, 2, 3);
|
||||||
|
System.out.println(intLIst);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCollectorsJoining_thenPrintCustom() {
|
||||||
|
List<Integer> intList = Arrays.asList(1, 2, 3);
|
||||||
|
System.out.println(intList.stream()
|
||||||
|
.map(n -> String.valueOf(n))
|
||||||
|
.collect(Collectors.joining("-", "{", "}")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenStringUtilsJoin_thenPrintCustom() {
|
||||||
|
List<Integer> intList = Arrays.asList(1, 2, 3);
|
||||||
|
System.out.println(StringUtils.join(intList, "|"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user