如何使用Java Comparator使用两个参数对ArrayList进行排序?
我使用Java Comparator按照单词频率属性的降序对单词对象的ArrayList进行排序。Word对象是通过首先使用hashmap从.txt文件中读取单词,然后将hashmap转换为Word对象的ArrayList来创建的。然后我想按字母顺序对频率相同的单词进行排序如何使用Java Comparator使用两个参数对ArrayList进行排序?,java,arraylist,comparator,Java,Arraylist,Comparator,我使用Java Comparator按照单词频率属性的降序对单词对象的ArrayList进行排序。Word对象是通过首先使用hashmap从.txt文件中读取单词,然后将hashmap转换为Word对象的ArrayList来创建的。然后我想按字母顺序对频率相同的单词进行排序 while (reader.hasNext()) { word = reader.next().toLowerCase(); word = word.substring(
while (reader.hasNext()) {
word = reader.next().toLowerCase();
word = word.substring(0, 1).toUpperCase() + word.substring(1);
word = word.replaceAll("[^a-zA-Z ]", "");
if (!word.contains("0") || !word.contains("1") || !word.contains("2") || !word.contains("3") || !word.contains("4") || !word.contains("5") || !word.contains("6") || !word.contains("7") || !word.contains("8") || !word.contains("9") || !word.contains("-") || !word.contains("_")) {
// This is equivalent to searching every word in the list via hashing (O(1))
if(!frequencyMap.containsKey(word)) {
frequencyMap.put(word, 1);
} else {
// We have already seen the word, increase frequency.
frequencyMap.put(word, frequencyMap.get(word) + 1);
}
}
counter++;
}
for(Map.Entry<String, Integer> entry : frequencyMap.entrySet()) {
Word word = new Word(entry.getKey());
word.frequency = entry.getValue();
wordList.add(word);
}
Collections.sort(wordList, Word.WordFrequency);
public class Word {
String value;
int frequency;
public Word(String v) {
value = v;
frequency = 1;
}
public String getValue() {
return value;
}
public int getFrequency() {
return frequency;
}
public static Comparator<Word> WordFrequency = new Comparator<Word>() {
public int compare(Word w1, Word w2) {
int w1Frequency = w1.getFrequency();
int w2Frequency = w2.getFrequency();
return w2Frequency-w1Frequency;
}
};
}
while(reader.hasNext()){
word=reader.next().toLowerCase();
word=word.substring(0,1).toUpperCase()+word.substring(1);
word=word.replaceAll(“[^a-zA-Z]”,“”);
如果(!word.contains(“0”)| |!word.contains(“1”)| |!word.contains(“2”)|!word.contains(“3”)|!word.contains(“4”)|!word.contains(“5”)|!word.contains(“6”)|!word.contains(“7”)|!word.contains(“8”)|!word.contains”(“9”)|!word.contains”| contains(“)| |{
//这相当于通过哈希(O(1))搜索列表中的每个单词
如果(!frequencyMap.containsKey(word)){
frequencyMap.put(字,1);
}否则{
//我们已经看到了“增加频率”这个词。
frequencyMap.put(字,frequencyMap.get(字)+1);
}
}
计数器++;
}
对于(Map.Entry:frequencyMap.entrySet()){
Word=新词(entry.getKey());
word.frequency=entry.getValue();
添加(word);
}
Collections.sort(wordList、Word.WordFrequency);
公共类词{
字符串值;
整数频率;
公共字(字符串v){
值=v;
频率=1;
}
公共字符串getValue(){
返回值;
}
公共int getFrequency(){
返回频率;
}
公共静态比较器WordFrequency=新比较器(){
公共整数比较(字w1,字w2){
int w1Frequency=w1.getFrequency();
int w2Frequency=w2.getFrequency();
返回W2频率-W1频率;
}
};
}
请参阅然后比较
方法,该方法允许您在出现以下情况时提供比较键:
// sort using 'd' will sort 1st alphabetically, then by length
// (this is a totally arbitrary example)
Comparator<String> c = String::compareTo;
Comparator<String> d = c.thenComparing(s -> s.length());
//使用“d”排序将首先按字母顺序排序,然后按长度排序
//(这是一个完全武断的例子)
比较器c=字符串::compareTo;
比较器d=c.然后比较(s->s.长度();
请参阅然后比较
方法,该方法允许您在出现以下情况时提供比较键:
// sort using 'd' will sort 1st alphabetically, then by length
// (this is a totally arbitrary example)
Comparator<String> c = String::compareTo;
Comparator<String> d = c.thenComparing(s -> s.length());
//使用“d”排序将首先按字母顺序排序,然后按长度排序
//(这是一个完全武断的例子)
比较器c=字符串::compareTo;
比较器d=c.然后比较(s->s.长度();
if(w2Frequency-w2Frequency==0){return/*compareTheStrings*/;}
可能与@luk2302重复感谢您的帮助,如果您想发布答案,下面是我的答案:if(w2Frequency==w2Frequency){return w1.getValue().compareTo(w2.getValue();}否则{return w2Frequency-w2Frequency;}
if(w2Frequency-w2Frequency==0){return/*compareTheStrings*/;}
可能与@luk2302重复感谢您的帮助,如果您想发布答案,下面是我的答案:if(w2Frequency==w2Frequency){return w1.getValue().compareTo(w2.getValue();}否则{return w2Frequency-w2Frequency;}