容器set(一)

本文介绍了Java中Set接口的四种主要实现:HashSet、TreeSet、LinkedHashSet和SortedSet。Set的特点是元素唯一且无特定输出顺序。HashSet注重快速查找,元素需重写hashCode();TreeSet保证元素排序,需实现Comparable接口;LinkedHashSet保持插入顺序,同时具备快速查找;SortedSet是TreeSet的扩展,提供额外操作如获取头尾元素、截取部分元素等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值