Comparable 和 TreeSet
程序员文章站
2022-05-14 13:07:06
...
import java.util.Set;
import java.util.TreeSet;
/**
* 测试TreeSet的使用(实际使用TreeMap实现的)
* 熟悉Comparable接口
* @author Administrator
*
*/
public class TestTreeSet {
public static void main(String[] args) {
Set<Integer> set = new TreeSet<>();
set.add(30);
set.add(100);
set.add(90);
for(Integer m:set) {
System.out.println(m);
}
Set<Employee> set2 = new TreeSet<>();
set2.add(new Employee(100,"张三",50000));
set2.add(new Employee(200,"李四",20000));
set2.add(new Employee(300,"王五",80000));
set2.add(new Employee(400,"赵六",20000));
for(Employee m:set2) {
System.out.println(m);
}
}
}
上一篇: web前端性能优化
下一篇: TreeSet源码解析
推荐阅读