标签:
特点:无序的,长度可变的,不可重复的。
HashSet 保证元素的唯一性是通过元素的两个方法,hashCode 和 equals 来完成。
-----如果元素的 HashCode 值相同,才会判断 equals 是否为true。
-----如果元素的 HashCode 值不同,不会调用equals。
-----注意:对于判断元素是否存在,以及删除等操作,依赖的方法是 hashcode 和 equals 方法。
import java.util.HashSet; import java.util.Iterator;
public class HashSetDemo {
public static void main(String[] args) { // TODO Auto-generated method stub HashSet hs = new HashSet(); // hs.add(new Students("v7","java")); hs.add(new Students("V7S", "java")); System.out.println(hs.contains(new Students("V7", "java"))); Iterator iterator = hs.iterator(); while (iterator.hasNext()) { Students ss = (Students) iterator.next(); System.out.println(ss.getName() + "----" + ss.getBook()); } }
}
class Students { public String name; public String book;
@Override public boolean equals(Object o) { if (this == o) { return true; }
if (o.getClass() == Students.class) { Students n = (Students) o; return n.book.equals(book); //只判断 book 字段 //return n.book.equals(book) && n.name.equals(name); } return false;
}
@Override public int hashCode() { // TODO Auto-generated method stub return this.book.hashCode(); }
public Students(String name, String book) { // TODO Auto-generated method stub this.name = name; this.book = book; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getBook() { return book; }
public void setBook(String book) { this.book = book; }
}
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; } }
java 集合框架 Set 集合之 HashSet TreeSet
标签:
原文地址:http://blog.csdn.net/qq_29689487/article/details/51350779