TreeSet中自定义Comparator实现降序
1 @Test 2 public void test1() { 3 TreeSet ts = new TreeSet<Integer>(new MyComparator()); 4 ts.add(3); 5 ts.add(2); 6 ts.add(100); 7 12 for(Iterator i = ts.iterator();i.hasNext();) 13 { 14 System.out.println(i.next()); 15 } 16 } 17 } 18 19 class MyComparator implements Comparator<Integer> { 20 21 public int compare(Integer o1, Integer o2) { 22 // TODO Auto-generated method stub 23 //降序排列 24 if(o1>o2) return -1; 25 if(o1==o2)return 0;else return 1; 26 } 27
posted on 2015-11-16 15:22 freedom's_blog 阅读(524) 评论(0) 编辑 收藏 举报