*在当前项目根目录下有一个“qq.txt文件”里面存放的内容如下:
(项目根目录,假设qq号的长度最大为11位)
12345
67891
12347809933
98765432102
67891
12347809933
a.将该文件里面的所有qq号都存放在list中
b.将list中重复元素删除
c.将剩余元素进行排序(按照长度由小到大)
d.将list中所有元素用两种方式打印出来
* @throws IOException
* @throws IOException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
List<String> ls = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader("qq.txt"));
String line;
while ((line = br.readLine()) != null) {
ls.add(line);
}
//去重复前
getSingle(ls);
System.out.println(ls);
sort(ls);
//去重复后
for (String string : ls) {
System.out.println(string);
}
}
private static void sort(List<String> ls) {
TreeSet<String> ts = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
int num = s1.length() - s2.length();
return num == 0 ? s1.compareTo(s2) : num;
}
});
}
private static void getSingle(List<String> ls) {
LinkedHashSet<String> lhs = new LinkedHashSet<>();
lhs.addAll(ls);
ls.clear();
ls.addAll(lhs);
} |
|