本文介绍了查找ArrayList和显示重复的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能帮助我吗?我需要编写一个程序,在那里我在的ArrayList 10个元素,我需要找到它有多少重复的值,并计算和WEL显示的值。

例如:说我有

 列表= {堆栈,溢出,堆栈,
        雅虎,谷歌,MSN,
        MSN,堆栈,溢出,用户}

结果应该是:

 堆栈= 3
溢出= 2
谷歌= 1
MSN = 2
雅虎= 1
用户= 1


解决方案

使用库的多集。它支持添加元素的整数倍,且多集包含每个元素的多少次计数。

 多重集<串GT; wordsMultiset = HashMultiset.create();
wordsMultiset.addAll(字);
对于(Multiset.Entry<串GT;项:wordsMultiset.entrySet()){
     的System.out.println(词:+ entry.getElement()+伯爵 - >中+ entry.getCount());
}

Can anybody help me? I need to write a program, where I have 10 elements in the arraylist and I need to find the how many duplicate values it has and count and display the values as wel.

Ex: say I have

list = {"stack", "overflow", "stack",
        "yahoo", "google", "msn",
        "MSN", "stack", "overflow", "user" }

Result should be:

stack = 3
overflow = 2
google = 1
msn = 2
yahoo =1
user = 1
解决方案

Use Google Guava library's MultiSet. It supports adding multiples of elements, and counting how many occurrences of each element the multiset contains.

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

这篇关于查找ArrayList和显示重复的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:02