-
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 HashCodeType extends TestType {
public HashCodeType(int index) {
super(index);
// TODO Auto-generated constructor stub
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return index;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
//判断是否是相同类比较。
if (obj instanceof TestType && ((TestType) obj).getIndex() == index){
return true;
}
else {
return false;
}
}
}
HashCodeType
public class ComparableType extends TestType implements Comparable<ComparableType>{
public ComparableType(int index) {
super(index);
// TODO Auto-generated constructor stub
}
@Override
public int compareTo(ComparableType o) {
// TODO Auto-generated method stub
if (index > o.getIndex()){
return -1;
}
else if (index == o.getIndex()){
return 0;
}
else {
return 1;
}
}
}
ComparableType
②、进行测试
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
fill(new HashSet<TestType>(), TestType.class);
fill(new LinkedHashSet<HashCodeType>(), HashCodeType.class);
fill(new TreeSet<>(), ComparableType.class);
}
public static <E> void fill(Set<E> set,Class<E> type ){
for(int i=0; i<10; ++i){
//通过反射创建类
try {
Constructor<E> constructor = type.getConstructor(int.class);
set.add(constructor.newInstance(i));
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(set.toString());
}
}
/**
*输出:
*[TestType [index=4], TestType [index=7], TestType [index=8], TestType [index=0], TestType [index=6], TestType [index=1], TestType [index=2], TestType [index=3], TestType [index=5], TestType [index=9]]
[TestType [index=0], TestType [index=1], TestType [index=2], TestType [index=3], TestType [index=4], TestType [index=5], TestType [index=6], TestType [index=7], TestType [index=8], TestType [index=9]]
[TestType [index=9], TestType [index=8], TestType [index=7], TestType [index=6], TestType [index=5], TestType [index=4], TestType [index=3], TestType [index=2], TestType [index=1], TestType [index=0]]
*/
Main