Set:
Set不保存重复元素。
Set中最常被使用的是测试归属性,可以很容易地询问某个对象是否在某个Set中。
正因为如此,查找就成为了Set中最重要的操作,因此通常都会选择HashSet的实现,它专门对快速查找进行了优化。
HashSet:
出于速度方面的考虑,HashSet使用的是散列函数,故遍历其元素,输出的顺序是无序的。
当需要向HashSet中放置自定义元素时,应该为要存放到散列表的各个对象定义hashCode()和equals()。否则,不能实现去重功能。
TreeSet:
TreeSet会对放入其中的元素自动进行排序。
那么,放入的元素就要能够排序:要么是元素自身能够排序(不是很常用),要么是给定一个排序规则(常用)。
通常,我们可以在构造TreeSet对象时,传递一个实现了Comparator接口的比较器对象来实现排序。
Set不保存重复元素。
Set中最常被使用的是测试归属性,可以很容易地询问某个对象是否在某个Set中。
正因为如此,查找就成为了Set中最重要的操作,因此通常都会选择HashSet的实现,它专门对快速查找进行了优化。
HashSet:
出于速度方面的考虑,HashSet使用的是散列函数,故遍历其元素,输出的顺序是无序的。
当需要向HashSet中放置自定义元素时,应该为要存放到散列表的各个对象定义hashCode()和equals()。否则,不能实现去重功能。
下面是一个例子。
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Student> myset = new HashSet<Student>();
myset.add(new Student("one", 18));
myset.add(new Student("one", 18));
myset.add(new Student("two", 18));
myset.add(new Student("two", 18));
myset.add(new Student("thrahasdgasgwseasdfgawerghasdgasdhasfgasdg", 18));
myset.add(new Student("thrahasdgasgwseasdfgawerghasdgasdhasfgasdg", 18));
for(Iterator<Student> it=myset.iterator(); it.hasNext(); ) {
Student stu = it.next();
System.out.printf("name:%s age:%d\n", stu.getName(), stu.getAge());
}
}
static class Student { //HashSet要重写hashCode和equals方法才能实现去重功能
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
public int hashCode() {
return name.hashCode()*7+age*13;
}
public boolean equals(Object o) { //must be Object
Student stu = (Student)o;
return this.name.equals(stu.name) && this.age==stu.age;
}
}
}
/*
name:thrahasdgasgwseasdfgawerghasdgasdhasfgasdg age:18
name:one age:18
name:two age:18
*/
TreeSet:
TreeSet会对放入其中的元素自动进行排序。
那么,放入的元素就要能够排序:要么是元素自身能够排序(不是很常用),要么是给定一个排序规则(常用)。
通常,我们可以在构造TreeSet对象时,传递一个实现了Comparator接口的比较器对象来实现排序。
需要注意的是,TreeSet是根据比较器的比较规则来去除重复元素的,所以,如果是自定义对象,那么在实现比较器的时候通常应该将对象的所有域进行排序。
下面是一个例子。
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class Test {
public static void main(String[] args) {
Set<Student> myset = new TreeSet<Student>(new MyComp());
myset.add(new Student("two", 20));
myset.add(new Student("one", 18));
myset.add(new Student("two", 18));
myset.add(new Student("one", 20));
myset.add(new Student("thr", 18));
myset.add(new Student("thr", 18));
for(Iterator<Student> it=myset.iterator(); it.hasNext(); ) {
Student stu = it.next();
System.out.printf("name:%s age:%d\n", stu.getName(), stu.getAge());
}
}
static class MyComp implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
int x = o1.getAge()-o2.getAge();
if(x == 0) { //如果注释此句,结果是?
return o1.getName().compareTo(o2.getName());
}
return x;
}
}
static class Student {
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
}
/*
name:one age:18
name:thr age:18
name:two age:18
name:one age:20
name:two age:20
*/
/*
如果注释那个if语句,结果是这样:
name:one age:18
name:two age:20
*/