如何讓樹狀按字母順序排序? 並刪除重複..它已經讓我堅持了一天。也許我需要得到一些睡眠..Treeset按字母順序排序
public static void main(String[] args) {
String fileName = args[0];
String words;
Scanner s = null;
Iterator itr;
try {
s = new Scanner(new BufferedReader(new FileReader(fileName)));
while (s.hasNext()) {
words = s.next();
TreeSet<String> ts = new TreeSet<String>();
ts.add(words);
System.out.println(ts);
}
} catch (FileNotFoundException fnfe) {
System.exit(0);
} finally {
if (s != null) {
s.close();
}
}
}
你有重複的東西嗎?在一個集合? – 2013-03-19 19:59:18
你正在爲每個單詞製作一個新的TreeSet ... – n00begon 2013-03-19 19:59:51
@KatjaChristiansen好吧,它*是*可能的 - 設計得很糟糕的'hashCode()'或'equals()' - 但我想它更加棘手'String' class :) – 2013-03-19 20:02:06