Java 在arraylist中找到重复的元素并显示

Java 在arraylist中找到重复的元素并显示,java,collections,arraylist,Java,Collections,Arraylist,有人能帮我吗?我需要编写一个程序,其中我在arraylist中有10个元素,我需要找到它有多少重复值,并计算和显示这些值 比如说我有 list = {"stack", "overflow", "stack", "yahoo", "google", "msn", "MSN", "stack", "overflow", "user" } 结果应该是: stack = 3 overflow = 2 google = 1 msn = 2 yahoo =1 user

有人能帮我吗?我需要编写一个程序,其中我在
arraylist
中有10个元素,我需要找到它有多少重复值,并计算和显示这些值

比如说我有

list = {"stack", "overflow", "stack", 
        "yahoo", "google", "msn", 
        "MSN", "stack", "overflow", "user" }
结果应该是:

stack = 3
overflow = 2
google = 1
msn = 2
yahoo =1
user = 1

使用hashmap

像这样:

Map<String, Integer> occurrencies = new HashMap<String, Integer>();
for (String word : list) {
    occurrencies.put(word, occurrencies.containsKey(word)
    ? occurrencies.get(word) + 1 : 1);
}
for (Entry<String, Integer> entry : occurrencies.entrySet()) {
    System.out.println("Word: "+entry.getKey()
                     + ", occurences: "+entry.getValue());
}
Map-occurrences=newhashmap();
for(字符串字:列表){
发生率.put(单词,发生率.containsKey(单词)
?发生率。获取(单词)+1:1;
}
for(条目:accurrencies.entrySet()){
System.out.println(“Word:+entry.getKey()
+,出现次数:“+entry.getValue());
}
使用库的
MultiSet
。它支持添加多个元素,并计算多元素集包含的每个元素的出现次数

Multiset<String> wordsMultiset = HashMultiset.create();
wordsMultiset.addAll(words);
for(Multiset.Entry<String> entry : wordsMultiset.entrySet() ){
     System.out.println("Word : "+entry.getElement()+" count -> "+entry.getCount());
}
Multiset单词Multiset=HashMultiset.create();
wordsMultiset.addAll(单词);
for(Multiset.Entry:wordsMultiset.entrySet()){
System.out.println(“Word:+entry.getElement()+”count->“+entry.getCount());
}

使用HashMap

Map<String, Integer> freqMap = new HashMap<String, Integer>();
Map freqMap=newhashmap();
创建一个
映射
,然后在
数组列表上迭代

然后针对每个元素:-

  • 如果该元素已存在于映射中,则将该元素的
    整数
    值增加1
  • 如果不存在,则将该元素添加为
    初始整数值
    为1的元素
Map frequency=newhashmap();
for(字符串元素:列表){
if(频率包含(元素)){
frequency.put(元素,frequency.get(元素)+1);
}
否则{
频率。put(元素,1);
}
}
for(Map.Entry:frequency.entrySet()){
System.out.print(entry.getKey()+“=”+entry.getValue()+”);
}
System.out.println();

使用HashMap。下面是一个简单的实现

List<String> strings = new ArrayList<String>();
strings.put("stack", "overflow", "stack", "yahoo", "google", "msn", "MSN", "stack", "overflow", "user");

Map<String, Integer> counts = new HashMap<String, Integer>();

for (String str : strings) {
    if (counts.containsKey(str)) {
        counts.put(str, counts.get(str) + 1);
    } else {
        counts.put(str, 1);
    }
}

for (Map.Entry<String, Integer> entry : counts.entrySet()) {
    System.out.println(entry.getKey() + " = " + entry.getValue());
}
List strings=new ArrayList();
put(“stack”、“overflow”、“stack”、“yahoo”、“google”、“msn”、“msn”、“stack”、“overflow”、“user”);
映射计数=新的HashMap();
for(字符串str:strings){
if(计数容器(str)){
counts.put(str,counts.get(str)+1);
}否则{
计数。放置(str,1);
}
}
对于(Map.Entry:counts.entrySet()){
System.out.println(entry.getKey()+“=”+entry.getValue());
}

请提供一个示例,说明您在发布SO之前尝试过的方法。是的-比使用HashMap的所有建议都要简单…简单,谢谢您的帮助:)
List<String> strings = new ArrayList<String>();
strings.put("stack", "overflow", "stack", "yahoo", "google", "msn", "MSN", "stack", "overflow", "user");

Map<String, Integer> counts = new HashMap<String, Integer>();

for (String str : strings) {
    if (counts.containsKey(str)) {
        counts.put(str, counts.get(str) + 1);
    } else {
        counts.put(str, 1);
    }
}

for (Map.Entry<String, Integer> entry : counts.entrySet()) {
    System.out.println(entry.getKey() + " = " + entry.getValue());
}