标签:object 容量 shc java 好的 应该 rgs this png
集合元素无序、不重复,三个实现类都是线程不安全的,最好在创建时通过Collections工具类的synchronizedSortedSet方法来包装Set集合,防止对set集合的意外非同步访问。
利用lambda表达式定制排序
import java.util.TreeSet;
public class Customer {
private String name;
private int age;
public Customer(int age) {
this.age = age;
}
public String toString() {
return "[Customer:" + age + "]";
}
public static void main(String[] args) {
TreeSet<Customer> tsCustomers = new TreeSet<>((o1,o2) -> {
Customer c1 = (Customer)o1;
Customer c2 = (Customer)o2;
return c1.age > c2.age ? -1
:c1.age < c2.age ? 1 : 0;
});
tsCustomers.add(new Customer(10));
tsCustomers.add(new Customer(20));
System.out.println(tsCustomers);
}
}
输出结果为:
标签:object 容量 shc java 好的 应该 rgs this png
原文地址:https://www.cnblogs.com/pycrab/p/8933313.html