【问题标题】:How can I display count of duplicates in arraylist如何在arraylist中显示重复数
【发布时间】:2019-05-06 13:47:29
【问题描述】:

考虑下面包含重复项的数组列表。我将如何过滤掉出现 4 次的整数 89 ,例如 89:4 。预期输出 89 : 4

List<Integer> list = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 21, 61, 98, 15, 25, 41, 67,55, 89, 89, 89, 89 );         
Map<Integer ,Long > map = list.stream()
        .collect(Collectors.groupingBy(c ->c , Collectors.counting())) ;
map.forEach(   (k , v ) -> System.out.println( k + " : "+ v ));

//我期待下面的输出 89 : 4

// 上面代码sn-p的实际输出(键,值)对如下 1:2 98:1 2:1 67:1 3:1 5:1 8 : 1 41:1 13:1 15:1 21:2 55:1 89 : 4 25 : 1 61:1

【问题讨论】:

  • 894 这里有什么特别之处?您是否尝试在列表中查找最常见的项目?
  • 我得到了答案列表 list = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 21, 61, 98, 15, 25, 41, 67 ,55, 89, 89, 89, 89 ); final Optional> max = list.stream().collect( Collectors.groupingBy(Function.identity(), Collectors.counting())) .entrySet().stream().max( Comparator.comparing(Map.Entry::getValue)); System.out.println(max.get());
  • 你可以保留任何整数 我对 89 不具体,用一些随机整数替换 89 我没问题@KorayTugay
  • @sekhar 您的问题标题与您的预期大相径庭。您的意思是要找到出现的最大整数及其计数吗?
  • 是的,在arraylist @nullpointer 中查找重复整数的最大出现次数及其计数

标签: java arraylist java-8 hashmap


【解决方案1】:

你可以这样做,

Entry<Integer, Long> maxOccurence = list.stream()
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
    .entrySet().stream()
    .max(Comparator.comparing(Map.Entry::getValue))
    .orElseThrow(IllegalArgumentException::new);

【讨论】:

  • 这只会打印 89。
  • 我需要打印 89 : 4 ,意思是 89 出现了 4 次,显示你喜欢 89:4 ,但打印的是 89?
  • @sekhar 这打印89=4
【解决方案2】:

如果你使用Eclipse Collections,你可以使用topOccurrences

List<Integer> list = 
    Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 21, 61, 98, 15, 25, 41, 67,55, 89, 89, 89, 89 );
MutableList<ObjectIntPair<Integer>> top = Lists.adapt(list).toBag().topOccurrences(1);
System.out.println(top.makeString());

如果你想要所有的重复,你可以使用selectDuplicates。这会过滤掉 1、21 和 89。

MutableBag<Integer> dupes = Lists.adapt(list).toBag().selectDuplicates();
System.out.println(dupes.toStringOfItemToCount());

您也可以在不对基元进行装箱的情况下执行此操作。

IntList list =
    IntLists.mutable.with(1, 1, 2, 3, 5, 8, 13, 21, 21, 61, 98, 15, 25, 41, 67, 55, 89, 89, 89, 89);

MutableList<IntIntPair> top = list.toBag().topOccurrences(1);
System.out.println(top.makeString());

IntBag dupes = list.toBag().selectDuplicates();
System.out.println(dupes.toStringOfItemToCount());

注意:我是 Eclipse Collections 的提交者。

【讨论】:

    【解决方案3】:

    另一种方式是这样的

    Map<Integer, Integer> temp = new HashMap<>();
    int maxCount = 0;
      for (Integer i : list) {
       maxCount = Integer.max(maxCount , temp.merge(i, 1, Integer::sum));
      }
    int finalMax = maxCount;
    temp.values().removeIf(v->v!= finalMax);
    


     Map<Integer, Integer> temp = new HashMap<>();
        int maxCount = 0;
        int maxKey = 0;
        for (Integer i : list) {
            int count = temp.merge(i, 1, Integer::sum);
            if (maxCount < count)
                maxKey = i;
            maxCount = Integer.max(maxCount , count);
        }
        System.out.println(maxKey + " :" + maxCount);
    

    【讨论】:

      【解决方案4】:

      简短且记忆友好的版本。它在 O(n^2) 时确实有一个糟糕的 big-O,但是对于小型列表,在具有 GC 压力的高并发环境中提供了最佳性能,因为 HashMaps 和 Map.Entry 对象占用空间并且对 CPU 缓存的使用不当:

      import static java.util.Collections.frequency;
      import static java.util.Comparator.comparingInt;
      ...
      List<Integer> list = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 21, 
              61, 98, 15, 25, 41, 67,55, 89, 89, 89, 89 );
      
      list.stream()
        .max(comparingInt(x -> frequency(list, x)))
        .map(x -> x + " : " + frequency(list,x))
        .ifPresent(System.out::println);
      

      打印89 : 4,即&lt;most frequent int&gt; : &lt;num occurences&gt;

      【讨论】:

      • 它是硬编码的,我的意思是 4 次出现是硬编码的
      • 是的,在阅读了讨论之后,我终于明白了这个问题。您应该在问题中说“最大发生次数”。要么提供更好/替代的答案,要么将其删除。
      【解决方案5】:

      您可以使用 Apache Bag

      定义一个集合,计算对象在集合中出现的次数。 假设您有一个包含 {a, a, b, c} 的 Bag。在 a 上调​​用 getCount(Object) 将返回 2,而调用 uniqueSet() 将返回 {a, b, c}。

      Example

       Bag<Integer> bag = new HashBag<>(
       Arrays.asList(1, 2, 3, 3, 3, 1, 4));         
       assertThat(2, equalTo(bag.getCount(1)));
      

      【讨论】:

        【解决方案6】:

        希望对你有帮助……

        List<Integer> list = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 21, 61,
                    98, 15, 25, 41, 67,55, 89, 89, 89, 89 );
        Optional<Map.Entry<Integer, Long>> max = list.stream()
                    .collect(Collectors.groupingBy(obj -> obj, Collectors.counting()))
                    .entrySet()
                    .stream()
                    .max(Comparator.comparing(Map.Entry::getValue));
        
        if(max.isPresent()) {
            System.out.println(max.get());
        }
        

        【讨论】:

        • 为什么要 89?因为89是最大值?还是 89 是最大值?
        • List list = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 21, 61, 98, 15, 25, 41, 67,55, 89、89、89、89); final Optional> max = list.stream().collect( Collectors.groupingBy(Function.identity(), Collectors.counting())) .entrySet().stream().max( Comparator.comparing(Map.Entry::getValue)); System.out.println(max.get());
        • 考虑其他一些数字(用一些随机整数代替 89),我对 89 并不具体,就像这个 List list = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 21, 61, 98, 15, 25, 41, 67,55, 99, 99, 99, 99);
        • 所以在这种情况下,将输出 99:4,因为最大出现次数为 4。对吧?
        猜你喜欢
        • 2016-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-14
        • 1970-01-01
        • 2013-08-05
        相关资源
        最近更新 更多