Last active
October 22, 2020 19:40
-
-
Save viveknaskar/c9ecb5fffc214beb0a8af0833427a99d to your computer and use it in GitHub Desktop.
Counting collection using streams enhances the performance of code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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