public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader("qq.txt"));
String str;
while((str=br.readLine())!=null){ //一行一行的读取添加到list集合中
list.add(str);
}
br.close();
TreeSet<String> ts = new TreeSet<>(new Comparator<String>() { //利用Treeset集合的去掉重复的,并按从长度从小到大排序
public int compare(String o1, String o2) {
if(o1.equals(o2)) {
return 0;
}else {
return 1;
}
}
});
for (String st1 : list) {
ts.add(st1);
}
for (String st2 : ts) { //增强for遍历
System.out.println(st2);
}
} |