-
1、Set的特点:①、存入Set的每个元素在Set中都是唯一的 ②、输出的时候不保证元素的次序
-
2、Set延生的四种类型:
①、HashSet:为快速查找而设计的,存入Set的元素必须实现hashCode()方法
②、TreeSet:输出的时候保证元素的优先顺序(不是按照输入时候的顺序,而是根据元素的大小)。存入Set的元素必须实现Comparable接口
③、LinkedHashSet:具有HashSet快速查找的特性。还能够按照输入Set的顺序输出数据, 存入Set的元素必须实现hashCode()方法
④、SortedSet:对TreeSet的扩展,扩展了一些方法。(获取头,尾元素的方法。截取容器中部分元素的方法等) -
3、对各种类型的使用。
步骤①、制作原始类TestType,制作HashCodeType继承TestType重写hashCode()方法,制作ComparableType继承TestType和Comparable接口
public class TestType {
protected int index;
public TestType(int index){
this.index = index;
}
public int getIndex(){
return index;
}
@Override
public String toString() {
return "TestType [index=" + index + "]";
}
}
TestType
public class HashCo