Java 8 Stream collect Sample

Java 8 Streams collect operation performs a mutable reduction operation on the stream elements. Stream elements are included into the result by updating rather than by replacing.

The Java 8 Stream collect() operation is a terminal operation. After the collect() operation is executed in parallel, multiple intermediate results may be instantiated, populated, and merged so as to maintain isolation of mutable data structures.

Stream.collect() Samples:

 

Stream.collect() & Collectors.toList() String Array to List

Sample code that uses the collect() operation along with the Collectors.toList() method to converts a String array into a List object.

public static void collectStringArrayList() {	

	   String[] jcdStringArray = {"Java", "Code", "Depot"};

       List<String> jcdList = Arrays.stream(jcdStringArray).collect(Collectors.toList());

       jcdList.forEach(item->System.out.println(item));      
}

Output

Java
Code
Depot


 

Stream.collect() & Collectors.toMap() ArrayList to Map

Sample code that uses the collect() operation along with the Collectors.toMap() method that converts the Widget objects into a Map object.

public static void collectArrayListMap() {
		
	List<Widget> jcdList = new ArrayList<>();
		
	jcdList.add(new Widget(10, "Small Widget"));
	jcdList.add(new Widget(20, "Medium Widget"));
	jcdList.add(new Widget(30, "Large Widget"));
		
	Map<Integer, String> jcdMap = jcdList.stream()
		.collect(Collectors.toMap(Widget::getSize, Widget::getName));
		
	jcdMap.forEach((key, value) -> System.out.println("Map_Key: " + key +", Map_Value: "+ value));
        
}

Output

Map_Key: 20, Map_Value: Medium Widget
Map_Key: 10, Map_Value: Small Widget
Map_Key: 30, Map_Value: Large Widget


 

Stream.collect() & Collectors.summarizingInt() Summary Stats

Sample code that uses the collect() operation along with the Collectors.summarizingInt() method to provide statistics on the items in the stream.

public static void collectIntArraySumStats() {
		
	List<Integer> jcdList = Arrays.asList(1,2,3,4,5);	  
	  	  
	IntSummaryStatistics jcdTotal = jcdList.stream()
		.collect(Collectors.summarizingInt(num -> num));
	  	  
	System.out.println("Summary Stats: "+jcdTotal);
}

Output

Summary Stats: IntSummaryStatistics{count=5, sum=15, min=1, average=3.000000, max=5}
.....


 

Stream.collect() & Collectors.joining() Join & Sorted

Sample code that uses the collect() operation along with the Collectors.joining() method to join the String Array into a single String.

public static void collectJoinListToString() {
	
	List<String> jcdList = Arrays.asList("Fred","Barney","Wilma","Betty");
		
	String flinstones=  jcdList.stream().sorted()
		.collect(Collectors.joining("|"));
		
	System.out.println("Joined & Sorted: "+ flinstones);
}

Output

Joined & Sorted: Barney|Betty|Fred|Wilma


 

Stream.collect() & Collectors.averagingInt() List Average

Sample code that uses the collect() operation along with the Collectors.averagingInt() method to determine the average of the items in the stream.

public static void collectAverageInt() {
	
	List<Integer> jcdList = Arrays.asList(10, 20, 30, 40, 50);
	
    	Double jcdAvg = jcdList.stream().collect(Collectors.averagingInt( num -> num ));
    	
    	System.out.println("Average: "+jcdAvg);
}

Output

Average: 30.0


 

Stream.collect() & Collectors.counting() Count Items

Sample code that uses the collect() operation along with the Collectors.counting() method to count the number of items in the stream.

public static void collectSimpleCountItems() {
		
	List<Integer> jcdList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
		
	long jcdTotal=  jcdList.stream().collect(Collectors.counting());
	       
	System.out.println("Total Elements: "+ jcdTotal);
}

Output

Total Items: 9


Add a Comment

Your email address will not be published. Required fields are marked *