使用Java';s内置的集合类,用于从列表中计算列表值中的每个(关键字)唯一元素

使用Java';s内置的集合类,用于从列表中计算列表值中的每个(关键字)唯一元素,java,list,set,Java,List,Set,给定一个可能包含重复项的列表(如下面的列表),我需要能够计算每个(关键字)唯一元素的数量 List<String> list = new ArrayList<String>(); Set<String> set = new HashSet<String>(); list.add("M1"); list.add("M1"); list.add("M2"); list.add("M3"); set.addAll(list); System.out.p

给定一个可能包含重复项的列表(如下面的列表),我需要能够计算每个(关键字)唯一元素的数量

List<String> list = new ArrayList<String>();
Set<String> set = new HashSet<String>();
list.add("M1");
list.add("M1");
list.add("M2");
list.add("M3");

set.addAll(list);
System.out.println(set.size());

您要查找的是
Map
数据结构,而不是
Set

差不多

for(iterating over something){ 
    Integer count =map.get(value); 
    if( count == null){
          map.put(value, 1);


    } else{
        count++;
        map.put(value, count);
    }

}

Map是唯一映射到value的数据结构

Set
对您没有帮助。在这种情况下,您需要一个映射:

List<String> list = new ArrayList<String>();
Set<String> set = new HashSet<String>();
list.add("M1");
list.add("M1");
list.add("M2");
list.add("M3");

// ...

Map<String, Integer> counts = new HashMap<String, Integer>();
for(String element: list) {
    int currentCount;
    if(counts.contains(element)) {
        currentCount = counts.get(element) + 1;
    } else {
        currentCount = 1;
    }
    counts.put(element, currentCount);
}

// ...

for(String element: counts.keySet()) {
    System.out.println("element: " + element + ", times appeared: " + counts.get(element));
}
List List=new ArrayList();
Set=newhashset();
列表。添加(“M1”);
列表。添加(“M1”);
列表。添加(“M2”);
列表。添加(“M3”);
// ...
映射计数=新的HashMap();
for(字符串元素:列表){
int电流计数;
if(计数.包含(元素)){
currentCount=计数。获取(元素)+1;
}否则{
currentCount=1;
}
counts.put(元素、当前计数);
}
// ...
for(字符串元素:counts.keySet()){
System.out.println(“元素:“+element+”,出现次数:“+counts.get(元素));
}

我认为您正在寻找类似的内容(我没有编译它,但它应该能让您朝着正确的方向前进):

List List=ArrayList();
映射计数=新的HashMap();
//用值填充列表。。。。
用于(字符串项:列表){
整数计数=计数。获取(项);
如果(计数=null){
//这是我们第一次看到这个项目,所以计数应该是一。
计数=1;
}否则{
//将计数增加一。
计数=计数+1;
}
计数。放置(项目,计数);
}
//把它们全部打印出来。
for(条目:counts.entrySet()){
System.out.println(entry.getValue()+“”+entry.getKey());
}

表示您想知道列表(List)中包含多少“M1”,多少“M2”,而不是使用set界面,您可以使用Map界面,因为Map包含键、值对格式,即Map数据结构

 Map<key,Value>
Map

更简单的方法:使用
Collections.frequency()

System.out.println(“M2:+Collections.frequency(列表,“M2”);

将输出


M2:1

我如何在Map中计数?或者在Map中添加相同的值?我以前从未使用过Map。)我建议重构代码,只在每个循环中调用Map.get()一次。count++看起来很可疑。它是一个整数,不是整数。这真的符合你的要求吗?我认为map.put(value,count+1)无论如何都会更容易阅读…count++对于
Integer
Cool是有效的。我不知道(在我看来总是可疑):-)我建议重构此代码,不要在同一次迭代中同时调用contains()和get()。此外,编写的代码会重复计数,而不计算每个项在列表中出现的次数-put(元素,0)应该被put(元素,1)。此外,打印循环不需要调用get()-只需在entrySet()上迭代即可。get/contains/etc.非常快,但不是免费的。@Rob关于您的第一条评论:全部有效,已修复。关于你的第二条评论:get()实际上被调用了。我的第二个建议是不要调用get()。您希望迭代映射中的所有条目,最有效的方法是迭代entrySet()。这样,您就不必在对每个键调用get()时计算每个键的hashcode()和equals()方法。Map.Entry很难看,但它基本上没有开销就完成了任务。@Rob我不打算更改这部分,因为代码还是可以工作的。但是我会在你第一个发现的时候给你的答案投票。这个答案并没有说要在地图上放什么。有关如何使用地图解决此问题的更多详细信息将使这成为一个更有用的答案。建议编辑问题标题:“如何计算列表中项目的出现次数?”或类似的内容。
List<String> list = ArrayList<>();
Map<String, Integer> counts = new HashMap<>();
// Fill list with values....

for (String item:list) {
    Integer count = counts.get(item);
    if (count == null) {
        // This is the first time we have seen item, so the count should be one.
        count = 1;
    } else {
        // Increment the count by one.
        count = count + 1;
    }
    counts.put(item, count);
}

// Print them all out.
for (Entry<String, Integer> entry : counts.entrySet()) {
    System.out.println(entry.getValue() + " " + entry.getKey());
}
 Map<key,Value>