Last active
August 11, 2019 20:54
-
-
Save john-nash-rs/3eea958a63867896e52c02bb7832548e to your computer and use it in GitHub Desktop.
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.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