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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 赵晓东 中级黑马   /  2012-12-21 16:32  /  1161 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 Object 于 2013-1-23 16:50 编辑
  1. /*Collection--HashSet,TreeSet*/

  2. import java.util.*;

  3. class Animal
  4. {
  5.         String name;
  6.         int age;

  7.         Animal(String name,int age)
  8.         {
  9.                 this.name = name;
  10.                 this.age = age;
  11.         }

  12.         public String getName()
  13.         {
  14.                 return name;
  15.         }
  16.         public int getAge()
  17.         {
  18.                 return age;
  19.         }
  20. }

  21. class Cat extends Animal implements Comparable<Cat>//实现Comparable让元素具有比较性
  22. {
  23.         Cat(String name,int age)
  24.         {
  25.                 super(name,age);
  26.         }

  27.         public int hashCode()
  28.         {
  29.                 return name.hashCode()+age*6;
  30.         }

  31.         public boolean equals(Object obj)
  32.         {
  33.                 if(!(obj instanceof Cat))
  34.                         throw new RuntimeException("不是Cat对象");
  35.                 Cat cat = (Cat) obj;
  36.                 return this.name.equals(cat.name) && this.age == cat.age;
  37.         }

  38.         public int compareTo(Cat c)
  39.         {
  40.                 int num = this.name.compareTo(c.name);
  41.                         if(num ==0)
  42.                                 return new Integer(age).compareTo(c.age);
  43.                 return num;
  44.         }
  45. }

  46. class Dog extends Animal
  47. {
  48.         Dog(String name,int age)
  49.         {
  50.                 super(name,age);
  51.         }

  52.         public int hashCode()
  53.         {
  54.                 return name.hashCode()+age*6;
  55.         }

  56.         public boolean equals(Object obj)
  57.         {
  58.                 if(!(obj instanceof Dog))
  59.                         throw new RuntimeException("不是dog对象");
  60.                 Dog dog = (Dog) obj;
  61.                 return this.name.equals(dog.name) && this.age == dog.age;
  62.         }
  63. }

  64. class Comp implements Comparator<Animal>//TreeSet集合比较器
  65. {
  66.         public int compare(Animal a,Animal a2)
  67.         {
  68.                 int num = new Integer(a.getAge()).compareTo(new Integer(a2.getAge()));
  69.                         if(num ==0)
  70.                                 return a.getName().compareTo(a2.getName());
  71.                 return num;
  72.         }
  73. }

  74. class AnimalSetTest
  75. {
  76.         public static void main(String[] args)
  77.         {
  78.                 HashSet<Cat> hs = new HashSet<Cat>();//HashSet集合元素无序,元素不可以重复,根据hashCode排序

  79.                 hs.add(new Cat("小白猫",2));
  80.                 hs.add(new Cat("小黑猫",3));
  81.                 hs.add(new Cat("小灰猫",1));
  82.                 hs.add(new Cat("大黑猫",4));

  83.                 printHashSet(hs);
  84.                 sop("---------------------------------");

  85.                 TreeSet<Cat> ts = new TreeSet<Cat>();//TreeSet集合根据compareTo的返回值进行排序
  86.                 ts.add(new Cat("小灰猫",1));//元素自身具备比较性,按照姓名排序
  87.                 ts.add(new Cat("小黑猫",3));
  88.                 ts.add(new Cat("小白猫",2));
  89.                 ts.add(new Cat("大黑猫",4));

  90.                 printTreeSet(ts);
  91.                 sop("---------------------------------");
  92.                
  93.                 TreeSet<Dog> ds = new TreeSet<Dog>(new Comp());//第二种方法,使用比较器,按照年龄排序
  94.                
  95.                 ds.add(new Dog("大黄狗",4));
  96.                 ds.add(new Dog("大灰狗",2));
  97.                 ds.add(new Dog("大黑狗",3));
  98.                 ds.add(new Dog("大白狗",1));
  99.                 printTreeSet(ds);

  100.         }
  101.         
  102.         public static void printHashSet(HashSet<? extends Animal> c)//HashSet集合迭代器
  103.         {
  104.                 Iterator<? extends Animal> lt = c.iterator();

  105.                 while(lt.hasNext())
  106.                 {
  107.                         Animal a = lt.next();
  108.                         sop("AnimalName = "+a.getName()+"  Age = "+a.getAge());
  109.                 }
  110.         }

  111.         public static void printTreeSet(TreeSet<? extends Animal> c)//TreeSet集合迭代器
  112.         {
  113.                 Iterator<? extends Animal> lt = c.iterator();

  114.                 while(lt.hasNext())
  115.                 {
  116.                         Animal a = lt.next();
  117.                         sop("AnimalName = "+a.getName()+"...Age = "+a.getAge());
  118.                 }
  119.         }

  120.         
  121.         public static void sop(Object obj)
  122.         {
  123.                 System.out.println(obj);
  124.         }
  125. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
奋斗的青春 + 1 赞一个!

查看全部评分

1 个回复

倒序浏览
用增强for循环可以实现:
如下:
List<Animals> list;
for(Object o:list){
system.out.println(o.toString);
system.out.println(o.getName());
}

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马