Java:按长度对单词列表进行排序,然后按字母顺序排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18885656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Sort a list of words by length, then by alphabetical order
提问by user1692517
I am told to have a list of words sorted by length, and those that have the same length are sorted by alphabetical order. This is what I have for the method that does that so far.
有人告诉我有一个按长度排序的单词列表,而那些长度相同的单词按字母顺序排序。到目前为止,这就是我所拥有的方法。
public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
TreeMap<String, Integer> s = new TreeMap<String, Integer>();
ArrayList<Integer> count = new ArrayList<Integer>();
String line;
int length;
while ((line = r.readLine()) != null) {
length = line.length();
s.put(line, length);
if (!count.contains(length)){
count.add(length);
}
}
Collections.sort(count);
System.out.println(count);
}
My mindset was to use a TreeMap to keep the String, and the length of the word as the key. I also have an ArrayList that keeps track of all the word's lengths without any duplicates, it's then sorted.
我的想法是使用 TreeMap 来保留字符串,并以单词的长度作为键。我还有一个 ArrayList 可以跟踪所有单词的长度,没有任何重复,然后对其进行排序。
I was hoping to somehow call on the TreeMap for the key value of 5, which would list all the words with 5 letters in it.
我希望以某种方式调用 TreeMap 的键值为 5,它会列出所有包含 5 个字母的单词。
I was wondering if I'm on the right track? I've been playing around for over an hour and can't seem to figure out what I should do after this. Am I approaching this from the right angle?
我想知道我是否在正确的轨道上?我已经玩了一个多小时,似乎无法弄清楚在这之后我应该做什么。我是从正确的角度接近这个吗?
回答by Sualeh Fatehi
The easiest way would be to write a Comparator<String>
. The Comparator<String>
would receive two words, and compare them. If the first was shorter than the second, it should return -1. If the second was shorter than the first, it would return 1. If they are the same length, it should call the default String
compareTo
method. You can then simply sort your list using this custom Comparator
.
最简单的方法是编写一个Comparator<String>
. TheComparator<String>
会收到两个词,并比较它们。如果第一个比第二个短,它应该返回 -1。如果第二个比第一个短,则返回 1。如果它们的长度相同,则应调用默认String
compareTo
方法。然后,您可以简单地使用此自定义对您的列表进行排序Comparator
。
回答by radai
you want to use a string comparator that compares by length 1st. like so:
您想使用按长度第 1 进行比较的字符串比较器。像这样:
public class LengthFirstComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
if (o1.length()!=o2.length()) {
return o1.length()-o2.length(); //overflow impossible since lengths are non-negative
}
return o1.compareTo(o2);
}
}
then you could simply sort your Strings by calling Collections.sort(yourStringList, new LengthFirstComparator());
那么你可以通过调用简单地对你的字符串进行排序 Collections.sort(yourStringList, new LengthFirstComparator());
回答by Java Devil
By far the easiest and best way is to write a custom comparator as other answers say.
到目前为止,最简单和最好的方法是像其他答案所说的那样编写自定义比较器。
But to do it a similar way you were attempting would be to make the length the key and rather then having a single string as the value have a list of all the words of that length. So a map of the form
但是要以您尝试的类似方式执行此操作,将长度设为键,而不是将单个字符串作为值,列出该长度的所有单词。所以一张地图的表格
Map<Integer,List<String>>
You could then call the key of any length and return a sorted list of words like this
然后你可以调用任意长度的键并返回这样一个排序的单词列表
Collections.sort(yourMap.get(theLength))
BUT far more complicated then just using a comparator
但是比仅使用比较器要复杂得多
回答by Masudul
You can do that using simple List. Try following code.
您可以使用简单的列表来做到这一点。尝试以下代码。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
*
* @author Masudul Haque
*/
public class LengthSort {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
list.add("cowa");
list.add("cow");
list.add("aow");
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if(o1.length()>o2.length()){
return 1;
}else{
return o1.compareTo(o2);
}
}
});
System.out.println(list);
}
}
回答by agomez
You can use Java 8's lamba utilities to make concise functions that prevent the clutter of using comparator classes, like so:
您可以使用 Java 8 的 Lamba 实用程序来创建简洁的函数,以防止使用比较器类的混乱,如下所示:
Collections.sort(words, (string1, string2) -> Integer.compare(string1.length(), string2.length());
-Example taken from Effective Javaby Joshua Bloch
- 来自Joshua Bloch 的Effective Java的示例