Java 8 Coding Programs

    Remove the duplicate elements from a list

    List<String> list = Arrays.asList("Java", "C", "Python", "C", "Java", "Go");
    
    List<String> distinctList = list.stream()
    			   .distinct()
    			   .collect(Collectors.toList());
    
    System.out.println(distinctList);
    //Output -> [Java, C, Python, Go]

    Note – distinct() method returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream.

      Find duplicate elements in a list

      Method -1

      List<Integer> list = Arrays.asList(2,10,2,5,15,10,2);
      	
      Set<Integer> set = new HashSet<>();
      Set<Integer> duplicates = list.stream()
      				.filter(num -> !set.add(num))
      				.collect(Collectors.toSet());
      				
      System.out.println(duplicates);
      //Output - [2,10]

      Method – 2

      List<Integer> list = Arrays.asList(2,10,2,5,15,10,2);
          
      Set<Integer> duplicates = list.stream()
      				.filter(num -> Collections.frequency(list, num) > 1)
      				.collect(Collectors.toSet());
      		
      System.out.println(duplicates);
      //Output - [2,10]

        Find the frequency of each element in a list

        List<Integer> list = Arrays.asList(1, 3,5,1,4,3);
        
        Map<Integer, Long> countMap = list.stream()
        				.collect(Collectors.groupingBy(Function.identity(), 
                               Collectors.counting()));
        
        System.out.println(countMap);
        //Output -> 1=2, 3=2, 4=1, 5=1}

        Find the frequency of each character in a string

        String str = "Programming";
        		
        Map<Character, Long> countMap = str.chars()
                .mapToObj(ch -> (char) ch)
                .collect(Collectors.groupingBy(Function.identity(),
                                  Collectors.counting()));
                                  
        System.out.println(countMap); 
        //Output -> {P=1, a=1, r=2, g=2, i=1, m=2, n=1, o=1}

        Find the second largest number in an integer list

        List<Integer> list = Arrays.asList(2,10,2,5,15,10,2);
        		
        int secLargest = list.stream()
        				.distinct()
        				.sorted(Collections.reverseOrder())
        				.skip(1)
        				.findFirst()
        				.get();
        
        System.out.println(secLargest);
        //Output -> 10

        Find the second smallest number in an integer list

        ListList<Integer> list = Arrays.asList(2,10,2,5,15,10,2);
        		
        int secSmallest = list.stream()
        				.distinct()
        				.sorted()
        				.skip(1)
        				.findFirst()
        				.get();
        
        System.out.println(secSmallest);
        //Output -> 2

        Find maximum 3 numbers in an integer list

        List<Integer> list = Arrays.asList(2,10,2,5,15,10,2);
        
        List<Integer> max3 = list.stream()
        				.distinct()
        				.sorted(Comparator.reverseOrder())
        				.limit(3)
        				.collect(Collectors.toList());
        
        System.out.println(max3);
        //Output -> [15, 10, 5]

        Find minimum 3 numbers in an integer list

        List<Integer> list = Arrays.asList(2,10,2,5,15,10,2);
        
        List<Integer> min3 = list.stream()
        				.distinct()
        				.sorted()
        				.limit(3)
        				.collect(Collectors.toList());
        
        System.out.println(min3);
        //Output -> [2, 5, 10]

        Scroll to Top