Java ArrayList中最常见的n个单词 [英] Most frequent n - words in java ArrayList
问题描述
我需要在ArrayList中找到最常用的单词(n个单词,所以如果n = 5,则最常用的5个单词).
I need to find the most frequent words (n words, so if n = 5, the most frequent 5 words) in an ArrayList.
private ArrayList<String> wordList = new ArrayList<String>();
public ArrayList<String> mostOften(int k)
{
ArrayList<String> lista = new ArrayList<String>();
Set<String> unique = new HashSet<String>(wordList);
for (String key : unique)
System.out.println(key + ": " + Collections.frequency(wordList, key));
return lista;
}
该函数需要返回最频繁单词的列表,并按频率排序.如果2个单词的频率相同,则需要按字母顺序对其进行排序.我已经发布了我尝试过的内容,但这只能找到频率,其余的我不知道该怎么做.有帮助吗?
The function needs to return a list of the most frequent words, sorted by frequency. If 2 words have the same frequency, I need to sort them alphabetaclly. I have posted what I tried, but this only finds the frequency and I do not know how to do the rest. Any help?
推荐答案
您可以编写一个用列表初始化的Comparator类.然后,您可以使用列表和Comparator调用Collections.sort(). 代码可能看起来像这样:
You could write a Comparator class which is initialized with the list. Then you could invoke Collections.sort() with the list and the Comparator. The code might look like this:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class FrequencyComparator implements Comparator<String>{
List<String> list;
@Override
public int compare(String o1, String o2) {
if (Collections.frequency(list, o1) > Collections.frequency(list, o2)){
return -1;
}else if (Collections.frequency(list, o1) < Collections.frequency(list, o2)){
return 1;
}else{
return o1.compareTo(o2);
}
}
public FrequencyComparator(List<String> list){
this.list = list;
}
public static void main(String[] args)
{
List<String> list = new ArrayList<String>();
list.add("Hello");
list.add("You");
list.add("Hello");
list.add("You");
list.add("Apple");
list.add("Apple");
list.add("Hello");
Set<String> unique = new HashSet<>(list);
List<String> uniqueList = new ArrayList<>(unique);
Collections.sort(uniqueList, new FrequencyComparator(list));
System.out.println(uniqueList);
//Take the most frequent 2 objects
System.out.println(uniqueList.subList(uniqueList.size() - 2, uniqueList.size());
}
}
这篇关于Java ArrayList中最常见的n个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!