[英]Java: Sort a list of words by length, then by alphabetical order
有人告訴我有一個按長度排序的單詞列表,那些具有相同長度的單詞按字母順序排序。 到目前為止,這就是我所擁有的方法。
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);
}
我的想法是使用 TreeMap 來保留字符串,並將單詞的長度作為鍵。 我還有一個 ArrayList 可以跟蹤所有單詞的長度,沒有任何重復,然后對其進行排序。
我希望以某種方式調用 TreeMap 的鍵值 5,這將列出其中包含 5 個字母的所有單詞。
我想知道我是否在正確的軌道上? 我已經玩了一個多小時了,似乎無法弄清楚在此之后我應該做什么。 我從正確的角度接近這個嗎?
最簡單的方法是編寫一個Comparator<String>
。 Comparator<String>
將收到兩個單詞,並進行比較。 如果第一個比第二個短,則應返回-1。 如果第二個比第一個短,則返回1.如果它們的長度相同,則應調用默認的String
compareTo
方法。 然后,您可以使用此自定義Comparator
對列表進行排序。
你想使用一個比較長度為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);
}
}
然后你可以通過調用Collections.sort(yourStringList, new LengthFirstComparator());
來簡單地對你的字符串進行排序Collections.sort(yourStringList, new LengthFirstComparator());
你可以使用簡單的List來做到這一點。 請嘗試以下代碼。
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);
}
}
到目前為止,最簡單和最好的方法是編寫自定義比較器,正如其他答案所說。
但要做到這一點,你嘗試的方法類似於將長度作為密鑰,而不是只有一個字符串,因為該值具有該長度的所有單詞的列表。 所以表格的地圖
Map<Integer,List<String>>
然后,您可以調用任意長度的鍵,並返回這樣的單詞的排序列表
Collections.sort(yourMap.get(theLength))
但比使用比較器要復雜得多
您可以使用Java 8的lamba實用程序來創建簡潔的函數,以防止使用比較器類的混亂,如下所示:
Collections.sort(words, (string1, string2) -> Integer.compare(string1.length(), string2.length());
- 來自Joshua Bloch的Effective Java的示例
如果你有這樣的句子 - Apple and grape are not vegetables 並且被告知根據長度排序,如果兩個或更多單詞相等,那么如果它必須按字母順序排序,那么代碼如下:
public class ExampleDemo {
public static void main(String[] args) {
String s = "Apple and grape are not vegetables";
ExampleDemo e = new ExampleDemo();
e.display(s);
}
public void display(String str) {
String[] st = str.split(" ");
List<String> list = new ArrayList<>();
for(String word: st){
list.add(word);
}
System.out.println("Before sorting: " + list);
Comparator<String> comparator = (s1,s2) -> Integer.compare(s1.length(), s2.length());
Collections.sort(list, comparator);
System.out.println("After sorting: " + list);
}
}
Output:
Before sorting: [Apple, and, grape, are, not, vegetables]
After sorting: [and, are, not, Apple, grape, vegetables]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.