Skip to content

Instantly share code, notes, and snippets.

@viveknaskar
Last active October 22, 2020 19:40
Show Gist options
  • Save viveknaskar/c9ecb5fffc214beb0a8af0833427a99d to your computer and use it in GitHub Desktop.
Save viveknaskar/c9ecb5fffc214beb0a8af0833427a99d to your computer and use it in GitHub Desktop.
Counting collection using streams enhances the performance of code
package com.viveknaskar;
import java.util.ArrayList;
import java.util.List;
public class CountingCollectionsUsingStreams {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Bruce");
list.add("Tony");
list.add("Clark");
list.add("Natasha");
list.add("Sachin");
/**
* Using stream with lambda expression
* Here stream(), filter() and count() operations are occurring parallelly
* Using stream operations greatly enhances the performance of the code
*/
long count = list.stream().filter(num -> num.length() > 5).count();
System.out.println("Total number of people whose names are more than 5 characters: "
+ count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment