特点:无序的,长度可变的,不可重复的。
HashSet 保证元素的唯一性是通过元素的两个方法,hashCode 和 equals 来完成。
-----如果元素的 HashCode 值相同,才会判断 equals 是否为true。
-----如果元素的 HashCode 值不同,不会调用equals。
-----注意:对于判断元素是否存在,以及删除等操作,依赖的方法是 hashcode 和 equals 方法。
class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Object o) { if (this == o) { return true; } if (o.getClass() == Name.class) { Name n = (Name)o; return n.first.equals(first) && n.last.equals(last); } return false; } } public class HashSetTest { public static void main(String[] args) { Set<Name> s = new HashSet<Name>(); s.add(new Name("abc", "123")); System.out.println( s.contains(new Name("abc", "123"))); } }
import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { // TODO Auto-generated method stub TreeSet ts = new TreeSet(); ts.add(new Student("v1", 10)); ts.add(new Student("v2", 11)); ts.add(new Student("v3", 12)); ts.add(new Student("v4", 13)); ts.add(new Student("v1", 11)); Iterator ite = ts.iterator(); while(ite.hasNext()){ Student st = (Student)ite.next(); System.out.println(st.getName()+"<-->"+st.getAge()); } } } class Student implements Comparable{ //实现 Comparable 接口,强制让元素具备比较性 private String name; private int age; public Student(String name,int age) { // TODO Auto-generated constructor stub this.name = name; this.age = age; } @Override public int compareTo(Object o) { // TODO Auto-generated method stub if(!(o instanceof Student)) throw new RuntimeException("不是学生类对象!"); Student stu = (Student)o; System.out.println(stu.age+"=========compareTo()====>"+stu.name); //主要因素 if(this.age > stu.age){ return 1; }else if(this.age == stu.age){ //次要因素 return this.name.compareTo(stu.name); } return -1; } 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; } }
import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { // TODO Auto-generated method stub TreeSet ts = new TreeSet(new myCompa()); ts.add(new Student("v1", 10)); ts.add(new Student("v2", 11)); ts.add(new Student("v3", 12)); ts.add(new Student("v4", 13)); ts.add(new Student("v1", 11)); Iterator ite = ts.iterator(); while(ite.hasNext()){ Student st = (Student)ite.next(); System.out.println(st.getName()+"<-->"+st.getAge()); } } } class myCompa implements Comparator{ @Override public int compare(Object o1, Object o2) { Student st1 = (Student)o1; Student st2 = (Student)o2; int num = st1.getName().compareTo(st2.getName()); if(num == 0){ return new Integer(st1.getAge()).compareTo(new Integer(st2.getAge())); } return num; } } class Student{ private String name; private int age; public Student(String name,int age) { // TODO Auto-generated constructor stub this.name = name; this.age = age; } 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; } }