package 集合框架4.hashSet; import java.util.HashSet; import java.util.Iterator; import 对象库.Person; import 对象库.Student; /** * hashSet集合: * 数据结构为哈希表;因此需要复写hashCode()方法,以此来进行容器内部元素排序 * 但是哈希值有几率会相同,元素却不同,类如:"ab"和"ba" ;所以为了确保Set集合 * 元素的唯一性,还要进行内容比较,因而需要覆写equals()方法;详见对象库.Person类 * 特点:无序;元素唯一; * */ public class HashSetDemo1 { public static void main(String[] args) { HashSet<Person> hs1 = new HashSet<Person>(); hs1.add(new Person("Mr.Smith",19)); hs1.add(new Person("Mr.Tom",17)); hs1.add(new Person("Mr.Cokeey",21)); hs1.add(new Person("Mr.Sky",18)); HashSet<Student> hs2 = new HashSet<Student>(); hs2.add(new Student("Mr.Wang",14)); hs2.add(new Student("Mr.Li",13)); hs2.add(new Student("Mr.Yang",15)); hs2.add(new Student("Mr.Anny",12)); hs1.addAll(hs2); for(Iterator<Person> it1 =hs1.iterator();it1.hasNext();){ Person p = it1.next(); System.out.println(p.getName()+":"+p.getAge()); } } }
package 集合框架3.treeset; import java.util.Iterator; import java.util.TreeSet; import 对象库.Person; import 比较器.comparator.ComparetorByAge; public class TreeSetDemo { public static void main(String[] args) { TreeSet<Person> ts = new TreeSet<Person>(new ComparetorByAge()); ts.add(new Person("lisi",21)); ts.add(new Person("wangwu",22)); ts.add(new Person("zhaoliu",25)); ts.add(new Person("caiqi",27)); Iterator<Person> it = ts.iterator(); while(it.hasNext()){ Person p =/*(Person)可以省略强转原因是:声明Iterator是已经泛型<Person>不是Person对象是不会被操作的*/it.next(); System.out.println(p.getName()+":"+p.getAge()); } } }
package 比较器.comparator; /**集合内添加比较器用于TreeSet集合,TreeMap集合*/ import java.util.Comparator; import 对象库.Person; public class ComparetorByAge implements Comparator<Person> { public static void main(String[] args) { } @Override public int compare(Person o1, Person o2) { Person p1 = (Person)o1; Person p2 = (Person)o2; int temp=p1.getAge()-p2.getAge();//大于0则P1大 return temp==0?p1.getName().compareTo(p2.getName()):temp; //如果temp==0证明年龄相同,则比较name大小String类中覆写了compareTo()方法;如果temp!=0则返回temp; } }
package 比较器.comparator; import java.util.Comparator; import 对象库.Person; public class ComparetorByName implements Comparator<Person> { public static void main(String[] args) { // TODO Auto-generated method stub } @Override public int compare(Person o1, Person o2) { // TODO Auto-generated method stub int temp=o1.getName().compareTo(o2.getName()); return temp==0?o1.getAge()-o2.getAge():temp; } }
package 对象库; public class Person { public class Student { } private String name; private int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } public Person() { super(); } @Override public boolean equals(Object obj) { if(this==obj){ return true; } if(!(obj instanceof Person)){ throw new ClassCastException("类型转换异常"); }else{ Person p = (Person)obj; return this.name==p.name; } } @Override public int hashCode() { // TODO Auto-generated method stub return getName().hashCode()+age*31; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }