集合】如何统计一个List集合中每个元素的数量

2020-11-12 21:30发布

6条回答
石丰源 - Code the future
2楼 · 2020-11-12 21:37

/**

* 使用Lambda表达式实现过滤统计

*/

public void countListItemNum(List list) {

// 使用Map存储List中每个元素的数量:

// key:元素   value:数量

Map result = new HashMap<>();

list.forEach(item->{

long count = list.stream().filter(f->

item.equals(f)).count();

result.put(item, count);

});

// 遍历结果map

result.forEach((k,v)->{

System.out.println(k + ":" + v);

});

}


无需指教
3楼 · 2020-11-13 08:35
  1. /**

  2.  * java统计List集合中每个元素出现的次数

  3.      * 例如frequencyOfListElements(["111","111","222"])

  4.  *  ->

  5.  * 则返回Map {"111"=2,"222"=1}

  6.  * @param items

  7.  * @return  Map

  8.  * @author wuqx

  9.  */

  10. public static Map frequencyOfListElements( List items ) {

  11. if (items == null || items.size() == 0) return null;

  12. Map map = new HashMap();

  13. for (String temp : items) {

  14. Integer count = map.get(temp);

  15. map.put(temp, (count == null) ? 1 : count + 1);

  16. }

  17. return map;

  18. }


小猪仔
4楼 · 2020-11-13 09:24


public static void main(String[] args) {

// 1   10

// 1   20

// 2   10

// 3   57

// 1   2

// 3   24

List strList = new ArrayList();

List one = new ArrayList();

List two = new ArrayList();

List three = new ArrayList();

strList.add(new int[]{1, 10});

strList.add(new int[]{1, 20});

strList.add(new int[]{2, 10});

strList.add(new int[]{3, 57});

strList.add(new int[]{1, 2});

strList.add(new int[]{3, 24});

for(int i=0;i

int[] temp = (int[]) strList.get(i);

if(temp[0]==1){

one.add(temp[1]);

}else if(temp[0]==2){

two.add(temp[1]);

}else if(temp[0]==3){

three.add(temp[1]);

}

}

System.out.println("1: "+one.get(0)+","+one.get(1)+","+one.get(2));

System.out.println("2: "+two.get(0));

System.out.println("3: "+three.get(0)+","+three.get(1));

}


何大侠
5楼 · 2020-11-13 11:32
  1. /**

  2.  * java统计List集合中每个元素出现的次数

  3.      * 例如frequencyOfListElements(["111","111","222"])

  4.  *  ->

  5.  * 则返回Map {"111"=2,"222"=1}

  6.  * @param items

  7.  * @return  Map

  8.  * @author wuqx

  9.  */

  10. public static Map frequencyOfListElements( List items ) {

  11. if (items == null || items.size() == 0) return null;

  12. Map map = new HashMap();

  13. for (String temp : items) {

  14. Integer count = map.get(temp);

  15. map.put(temp, (count == null) ? 1 : count + 1);

  16. }

  17. return map;



我是大脸猫
6楼 · 2020-11-13 13:50


 

  1. /**

  2.  * java统计List集合中每个元素出现的次数

  3.      * 例如frequencyOfListElements(["111","111","222"])

  4.  *  ->

  5.  * 则返回Map {"111"=2,"222"=1}

  6.  * @param items

  7.  * @return  Map

  8.  * @author wuqx

  9.  */

  10. public static Map frequencyOfListElements( List items ) {

  11. if (items == null || items.size() == 0) return null;

  12. Map map = new HashMap();

  13. for (String temp : items) {

  14. Integer count = map.get(temp);

  15. map.put(temp, (count == null) ? 1 : count + 1);

  16. }

  17. return map;

  18. }


相关问题推荐

  • 回答 5

    Java提供的众多集合类由两大接口衍生而来:Collection(单列集合)接口和Map(双列集合)接口一 、Collection接口Collection接口定义了一个包含一批对象的集合。接口的主要方法包括:size() - 集合内的对象数量add(E)/addAll(Collection) - 向集合内添加单个/批...

  • 回答 5

    集合的话,分单列集合和双列集合,单列集合的话就是Collection接口,下面有两个子接口:Set和List。其中呢Set集合是一个不可有重复元素的无序集合;List是一个有序的集合,可以包含重复元素并且提供了按索引访问的方式。双列集合的话就是Map接口,它的实现类...

  • 回答 6

    list元素可重复,set元素唯一,map存储键值对。ArrayList实现原理是数组,是非线程安全的,同样效果线程安全使用vector。LinkedList实现原理是链表,是非线程安全的,线程安全使用ConcurrentLinkedQueue。HashSet实现原理是哈希表,元素是无序的。TreeSet实现...

  • 回答 3

    list元素可重复,set元素唯一,map存储键值对。ArrayList实现原理是数组,是非线程安全的,同样效果线程安全使用vector。LinkedList实现原理是链表,是非线程安全的,线程安全使用ConcurrentLinkedQueue。HashSet实现原理是哈希表,元素是无序的。TreeSet实现...

  • 回答 5

    Java集合框架为程序员提供了预先包装的数据结构和算法来操纵他们。集合是一个对象,可容纳其他对象的引用。集合接口声明对每一种类型的集合可以执行的操作。集合框架的类和接口均在java.util包中。任何对象加入集合类后,自动转变为Object类型,所以在取出的...

  • 回答 11

    Java中的集合主要分为四类:1、List列表:有序的,可重复的;2、Queue队列:有序,可重复的;3、Set集合:不可重复;4、Map映射:无序,键唯一,值不唯一。

没有解决我的问题,去提问