Skip to content

Instantly share code, notes, and snippets.

@john-nash-rs
Last active August 11, 2019 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john-nash-rs/3eea958a63867896e52c02bb7832548e to your computer and use it in GitHub Desktop.
Save john-nash-rs/3eea958a63867896e52c02bb7832548e to your computer and use it in GitHub Desktop.
package com.company;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class StreamsExample {
public static void main(String[] args) {
List<String> stringList =
Arrays.asList("11","2", "3", "2", "2", "6", "11", "3");
findUniqueUsingForLoopSequential(stringList);
System.out.println("\n");
stringList.stream().distinct().forEach(distinctString -> System.out.println("Sequentially using Stream is "+distinctString));
System.out.println("\n");
stringList.parallelStream().distinct().forEach(distinctString -> System.out.println("Parallel using Stream is "+distinctString));
}
private static void findUniqueUsingForLoopSequential(List<String> stringList) {
List<String> uniqueStringList = new ArrayList<>();
for (String str : stringList){
if(!uniqueStringList.contains(str)) {
uniqueStringList.add(str);
System.out.println("Sequentially using for loop : Unique String in the list is "+str);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment