2.当两个比较器都有时那个优先级高 我测试了 comparator 覆盖comparable 也就是说如果都有要执行comparator的比较器复制代码
- public class Test {
- public static void main(String[] args) {
- Collection c = new TreeSet(new myComparator());
- c.add(new Person("zhangsa", 15));
- c.add(new Person("zhangsan", 12));
- // c.add("nihao");
- // c.add("nihoa1");
- for (Iterator it = c.iterator(); it.hasNext();) {
- System.out.println(it.next());
- }
- }
- }
- class myComparator implements Comparator {
- @Override
- public int compare(Object o1, Object o2) {
- Person p1 = (Person) o1;
- Person p2 = (Person) o2;
- return p1.getAge() - p2.getAge();
- }
- }
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |