SHARE
TWEET

Untitled

a guest Aug 23rd, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6.  
  7. public class StreamsExample {
  8.     public static void main(String[] args) {
  9.         List<String> stringList =
  10.                 Arrays.asList("11","2", "3", "2", "2", "6", "11", "3");
  11.  
  12.         findUniqueUsingForLoopSequential(stringList);
  13.         System.out.println("\n");
  14.        
  15.         stringList.stream().distinct().forEach(distinctString -> System.out.println("Sequentially using Stream is "+distinctString));
  16.         System.out.println("\n");
  17.        
  18.         stringList.parallelStream().distinct().forEach(distinctString -> System.out.println("Parallel using Stream is "+distinctString));
  19.     }
  20.  
  21.     private static void findUniqueUsingForLoopSequential(List<String> stringList) {
  22.         List<String> uniqueStringList = new ArrayList<>();
  23.         for (String str : stringList){
  24.             if(!uniqueStringList.contains(str)) {
  25.                 uniqueStringList.add(str);
  26.                 System.out.println("Sequentially using for loop : Unique String in the list is "+str);
  27.             }
  28.         }
  29.  
  30.  
  31.     }
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top