做编程题时,需要对输入的对象进行排序,我就直接想到TreeSet这个类,代码逻辑如下:
public class TestTreeSet {
public static void main(String[] args) {
TreeSet<SortedModel> set=new TreeSet<>((a,b)->a.count-b.count);
set.add(new SortedModel('a',1));
set.add(new SortedModel('b',2));
set.add(new SortedModel('c',1));
set.stream().forEach(System.out::println);
}
}
class SortedModel {
char aChar;
int count;
public SortedModel(char aChar, int count) {
this.aChar = aChar;
this.count = count;
}
@Override
public String toString() {
return "SortedModel{" +
"aChar=" + aChar +
", count=" + count +
'}';
}
}
就是一个简单的Set中存入一些自定义对象,需要对自定义对象进行排序,然后输出。
上述代码期望的输出结果是&#