A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 122125241 中级黑马   /  2015-7-8 15:10  /  305 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package it.cast2;

  2. import java.util.Comparator;
  3. import java.util.Iterator;
  4. import java.util.TreeSet;

  5. public class TreeSetDemo {

  6.         public static void main(String[] args) {
  7.                
  8.                 Comparator<TestStudet> myComparetor = new Comparator<TestStudet>() {
  9.                         @Override
  10.                         public int compare(TestStudet o1, TestStudet o2) {
  11.                                
  12.                                 int result = o1.getAge() - o2.getAge();
  13.                                 if (result == 0) {
  14.                                         result = o1.getNameString().compareTo(o2.getNameString());
  15.                                 }
  16.                                 return result;
  17.                         }
  18.                 };
  19.                
  20.                 //MyComparetor myComparetor = new MyComparetor();
  21.                 TreeSet<TestStudet> tSet = new TreeSet<>(myComparetor);

  22.                 TestStudet testStudet = new TestStudet("1zhangsan", 23);
  23.                 TestStudet testStudet1 = new TestStudet("1zhangsan2", 232);
  24.                 TestStudet testStudet2 = new TestStudet("1zhangsan1", 232);
  25.                

  26.                 tSet.add(testStudet);
  27.                 tSet.add(testStudet1);
  28.                 tSet.add(testStudet2);

  29.                 Iterator<TestStudet> iterator = tSet.iterator();
  30.                 while (iterator.hasNext()) {
  31.                         TestStudet testStudet3 = iterator.next();
  32.                         System.out.println(testStudet3);
  33.                 }
  34.         }

  35. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马