Java 8 – How to split a String and Collect to any Collection ?

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:

  1. Split a String using comma (,) as delimiter and collect to List using Collectors.toList();
  2. Split a String using colon (:) as delimiter and collect to Set using Collectors.toSet();
  3. 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

SplitStringAndCollectToListUsingJava8Stream.java

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:

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

SplitStringAndCollectToSetUsingJava8Stream.java

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:

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

SplitStringAndCollectToCollectionUsingJava8Stream.java

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:

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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – Count and print number of lines and words in a text file
Java 8 – Find First and Last entries in a Map or HashMap ?