In this article, we will discuss how to split a String using different delimiters and collect to any Collection or List or Set
Split a String & Collect to List/Set/Collection :
- Split a String using comma (,) as delimiter and collect to List using Collectors.toList();
- Split a String using colon (:) as delimiter and collect to Set using Collectors.toSet();
- Split a String using pipe (|) as delimiter and collect to any Collection using Collectors.toCollection();
1. Split a String using comma as delimiter & Collect to List :
- Initially, we got a String with comma-separated values
- Inside Arrays.stream(), split a String using comma as delimiter using String’s split() method
- Then map a split-ted String using Stream.map() method to remove white-spaces if any
- Collect split-ted String to List using Stream’s collect() method passing Collectors.toList() as argument
- Finally, iterate & print List to console using List.forEach() method
SplitStringAndCollectToListUsingJava8Stream.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package in.bench.resources.split.string; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class SplitStringAndCollectToListUsingJava8Stream { public static void main(String[] args) { // original string String fruits = "Grapes, Apple, Mango, Banana, Orange, Melons" ; System.out.println( "Original comma-separted String :- \n" + fruits); // split String based on comma List<String> fruitList = Arrays.stream(fruits.split( "\\," )) // split on comma .map(str -> str.trim()) // remove white-spaces .collect(Collectors.toList()); // collect to List // print to console System.out.println( "\nIterating & printing split-ted String from List :- " ); fruitList.forEach(System.out::println); } } |
Output:
1 2 3 4 5 6 7 8 9 10 | Original comma-separted String :- Grapes, Apple, Mango, Banana, Orange, Melons Iterating & printing split-ted String from List :- Grapes Apple Mango Banana Orange Melons |
2. Split a String using colon as delimiter & Collect to Set :
- Initially, we got a String with colon-separated values
- Inside Arrays.stream(), split a String using colon as delimiter using String’s split() method
- Then map a split-ted String using Stream.map() method to remove white-spaces if any
- Collect split-ted String to Set using Stream’s collect() method passing Collectors.toSet() as argument
- Finally, iterate & print Set to console using Set.forEach() method
SplitStringAndCollectToSetUsingJava8Stream.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package in.bench.resources.split.string; import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; public class SplitStringAndCollectToSetUsingJava8Stream { public static void main(String[] args) { // original string String fruits = "Grapes:Apple:Mango:Banana:Orange:Melons" ; System.out.println( "Original colon-separted String :- \n" + fruits); // split String based on comma Set<String> fruitList = Arrays.stream(fruits.split( "\\:" )) // split on colon .map(str -> str.trim()) // remove white-spaces .collect(Collectors.toSet()); // collect to Set // print to console System.out.println( "\nIterating & printing split-ted String from Set :- " ); fruitList.forEach(System.out::println); } } |
Output:
1 2 3 4 5 6 7 8 9 10 | Original colon-separted String :- Grapes:Apple:Mango:Banana:Orange:Melons Iterating & printing split-ted String from Set :- Apple Grapes Melons Mango Orange Banana |
3. Split a String using Pipe as delimiter & Collect to any Collection :
- Initially, we got a String with pipe-separated values
- Inside Arrays.stream(), split a String using pipe as delimiter using String’s split() method
- Then map a split-ted String using Stream.map() method to remove white-spaces if any
- Collect split-ted String to any Collection classes using Stream’s collect() method passing Collectors.toCollection(TreeSet::new) as argument
- Finally, iterate & print Collection to console using Set.forEach() method
SplitStringAndCollectToCollectionUsingJava8Stream.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package in.bench.resources.split.string; import java.util.Arrays; import java.util.TreeSet; import java.util.stream.Collectors; public class SplitStringAndCollectToCollectionUsingJava8Stream { public static void main(String[] args) { // original string String fruits = "Grapes|Apple|Mango|Banana|Orange|Melons" ; System.out.println( "Original pipe-separted String :- \n" + fruits); // split String based on comma TreeSet<String> fruitList = Arrays.stream(fruits.split( "\\|" )) // split on pipe .map(str -> str.trim()) // remove white-spaces .collect(Collectors.toCollection(TreeSet:: new )); // collect to Collection // print to console System.out.println( "\nIterating & printing split-ted String from Collection :- " ); fruitList.forEach(System.out::println); } } |
Output:
1 2 3 4 5 6 7 8 9 10 | Original pipe-separted String :- Grapes|Apple|Mango|Banana|Orange|Melons Iterating & printing split-ted String from Collection :- Apple Banana Grapes Mango Melons Orange |
Related Articles :
- How to split String based on space & dot(.) as delimiter ?
- How to split String using pipe(|) as delimiter ?
- Java 8 – Reverse each words in a String using Stream and Collectors
- Java 8 – Reverse complete/entire String using Stream and Collectors
- Java 8 – Count and print number of lines and words in a text file
- Java 8 – Count and print number of repeated word occurrences in a text file
- Java 8 – Count and print number of repeated character occurrences in a String
References :
- https://www.benchresources.net/java-8-array-to-stream-conversion/
- https://www.benchresources.net/various-ways-to-split-string-in-java-3-ways/
- https://www.benchresources.net/java-how-to-split-string-using-pipe-delimiter/
- https://www.benchresources.net/java-8-stream-map-method-with-examples/
- https://www.benchresources.net/java-string-trim-method/
- https://www.benchresources.net/iterating-list-using-foreach-in-java-8/
- https://www.benchresources.net/iterating-set-using-foreach-in-java-8/
- https://www.benchresources.net/java-8-stream-collect-method-with-examples/
Happy Coding !!
Happy Learning !!