How to count duplicate elements in ArrayList?

前端 未结 10 1585
悲哀的现实
悲哀的现实 2020-12-15 11:55

I need to separate and count how many values in arraylist are the same and print them according to the number of occurrences.

I\'ve got an arraylist called digits :<

10条回答
  •  别那么骄傲
    2020-12-15 12:20

    The question is to count how many ones twos and threes are there in an array. In Java 7 solution is:

    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    public class howMany1 {
    public static void main(String[] args) {
    
        List list = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765);
    
        Map map = new HashMap<>();
    
          for(  Integer r  : list) {
              if(  map.containsKey(r)   ) {
                     map.put(r, map.get(r) + 1);
              }//if
              else {
                  map.put(r, 1);
              }
          }//for
    
          //iterate
    
          Set< Map.Entry > entrySet = map.entrySet();
          for(    Map.Entry  entry : entrySet     ) {
              System.out.printf(   "%s : %d %n "    , entry.getKey(),entry.getValue()  );
          }//for
    
    }}
    

    In Java 8, the solution to the problem is :

    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.stream.Collectors;
    
    public class howMany2 {
    public static void main(String[] args) {
    
        List list = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765);
         // we can also use Function.identity() instead of c->c
        Map map = list.stream()
                .collect(  Collectors.groupingBy(c ->c , Collectors.counting())         ) ;
    
    
        map.forEach(   (k , v ) -> System.out.println( k + " : "+ v )                    );
    
    }}
    

    One another method is to use Collections.frequency. The solution is:

    import java.util.Arrays;
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    
    public class Duplicates1 {
    public static void main(String[] args) {
    
        List list = Arrays.asList(1, 1, 2, 3, 5, 8, 13,13, 21, 34, 55, 89, 144, 233);
    
        System.out.println("Count all with frequency");
        Set set = new HashSet(list);
        for (Integer r : set) {
            System.out.println(r + ": " + Collections.frequency(list, r));
        }
    
    }}
    

    Another method is to change the int array to Integer List using method => Arrays.stream(array).boxed().collect(Collectors.toList()) and then get the integer using for loop.

    public class t7 {
        public static void main(String[] args) {
            int[] a = { 1, 1, 2, 3, 5, 8, 13, 13 };
            List list = Arrays.stream(a).boxed().collect(Collectors.toList());
    
            for (Integer ch : list) {
                System.out.println(ch + " :  " + Collections.frequency(list, ch));
            }
    
        }// main
    }
    

提交回复
热议问题