Set的里面不容许有重复的,如果是对象的话,就要给它一个比较的规则 ,否则默认情况下就通过equals来比较
Set set = new TreeSet(new MyComparator());
set.add(new Person("zhangsan"));
set.add(new Person("lisi"));
class MyComparator implements Comparator{
@Override
public int compare(Object o1, Object o2) {
Person p1 = (Person)o1;
Person p2 = (Person)o2;
//String的compareTo方法,对字符串一个一个的进行比较
return p1.getName().compareTo(p2.getName());
}
}