Java 如何在arraylist中显示重复计数

Java 如何在arraylist中显示重复计数,java,arraylist,java-8,hashmap,Java,Arraylist,Java 8,Hashmap,考虑以下由重复项组成的arraylist。我如何过滤掉发生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(Collector

考虑以下由重复项组成的arraylist。我如何过滤掉发生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 ));
List List=Arrays.asList(1,1,2,3,5,8,13,21,21,61,98,15,25,41,67,55,89,89,89,89);
Map=list.stream()
.collect(Collectors.groupingBy(c->c,Collectors.counting());
map.forEach((k,v)->System.out.println(k+“:”+v));
//我期待下面的输出 89:4

//上述代码段的实际输出(键、值)对,如下所示 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

你可以这样做

Entry<Integer, Long> maxOccurence = list.stream()
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
    .entrySet().stream()
    .max(Comparator.comparing(Map.Entry::getValue))
    .orElseThrow(IllegalArgumentException::new);
Entry maxoccurrence=list.stream()
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting())
.entrySet().stream()
.max(Comparator.comparing(Map.Entry::getValue))
.orelsetrow(IllegalArgumentException::new);
你可以这样做

Entry<Integer, Long> maxOccurence = list.stream()
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
    .entrySet().stream()
    .max(Comparator.comparing(Map.Entry::getValue))
    .orElseThrow(IllegalArgumentException::new);
Entry maxoccurrence=list.stream()
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting())
.entrySet().stream()
.max(Comparator.comparing(Map.Entry::getValue))
.orelsetrow(IllegalArgumentException::new);
希望,这将有助于

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());
}
List List=Arrays.asList(1,1,2,3,5,8,13,21,21,61,
98, 15, 25, 41, 67,55, 89, 89, 89, 89 );
可选max=list.stream()
.collect(Collectors.groupingBy(obj->obj,Collectors.counting())
.entrySet()
.stream()
.max(Comparator.comparing(Map.Entry::getValue));
如果(最大isPresent()){
System.out.println(max.get());
}
希望,这将有助于

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());
}
List List=Arrays.asList(1,1,2,3,5,8,13,21,21,61,
98, 15, 25, 41, 67,55, 89, 89, 89, 89 );
可选max=list.stream()
.collect(Collectors.groupingBy(obj->obj,Collectors.counting())
.entrySet()
.stream()
.max(Comparator.comparing(Map.Entry::getValue));
如果(最大isPresent()){
System.out.println(max.get());
}

简短且便于记忆的版本。它确实在O(n^2)处有一个糟糕的大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);
导入静态java.util.Collections.frequency;
导入静态java.util.Comparator.comparingInt;
...
列表=数组.asList(1,1,2,3,5,8,13,21,21,
61, 98, 15, 25, 41, 67,55, 89, 89, 89, 89 );
list.stream()
.max(比较(x->频率(列表,x)))
.map(x->x+:“+频率(列表,x))
.ifPresent(System.out::println);

打印
89:4

简短且内存友好的版本。它确实在O(n^2)处有一个糟糕的大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);
导入静态java.util.Collections.frequency;
导入静态java.util.Comparator.comparingInt;
...
列表=数组.asList(1,1,2,3,5,8,13,21,21,
61, 98, 15, 25, 41, 67,55, 89, 89, 89, 89 );
list.stream()
.max(比较(x->频率(列表,x)))
.map(x->x+:“+频率(列表,x))
.ifPresent(System.out::println);
打印
89:4

如果您使用,您可以使用
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());
也可以在不装箱基本体的情况下执行此操作

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());
IntList列表=
IntLists.mutable.with(1,1,2,3,5,8,13,21,21,61,98,15,25,41,67,55,89,89,89,89);
MutableList top=list.toBag().topOccurrences(1);
System.out.println(top.makeString());
IntBag dupes=list.toBag()。选择Duplicates();
System.out.println(dups.toStringOfItemToCount());
注意:我是Eclipse集合的提交者。

如果您使用,您可以使用
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());
也可以在不装箱基本体的情况下执行此操作

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());
IntList列表=
IntLists.mutable.with(1,1,2,3,5,8,13,21,21,61,98,15,25,41,67,55,89,89,89,89);
MutableList top=list.toBag().topOccurrences(1);
System.out.println(top.makeString());
IntBag dupes=list.toBag()。选择Duplicates();
System.out.println(dups.toStringOfItemToCount());

注意:我是Eclipse集合的提交者。

另一种方式是这样的

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 temp=newhashmap();
int maxCount=0;
for(整数i:列表){
maxCount=Integer.max(maxCount,temp.merge(i,1,Integer::sum));
}
int finalMax=最大计数;
temp.values().removeIf(v->v!=finalMax);


Map temp=newhashmap();
int maxCount=0;
int maxKey=0;
for(整数i:列表){
int count=temp.merge(i,1,Integer::sum);
如果(最大计数<计数)
maxKey=i;
maxCount=整数.max(maxCount,count);
}
System.out.println(maxKey+“:”+maxCount);

另一种方法是这样的

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 temp=newhashmap();
int maxCount=0;
for(整数i:列表){
maxCount=Integer.max(maxCount,temp.merge(i,1,Integer::sum));
}
int finalMax=最大计数;
temp.values().removeIf(v->v!=finalMax);


Map temp=newhashmap();
int maxCount=0;
int maxKey=0;
for(整数i:列表){
int count=temp.merge(i,1,Integer::sum);
如果(最大计数<计数)
maxKey=i;
maxCount=整数.max(maxCount,count);
}
System.out.println(maxKey+“:”+maxCount);
您可以使用Apache

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

行李b