HashSet有序还是无序,如何改为顺序输出

HashSet有序还是无序,如何改为顺序输出

众所周知,HashSet本质就是HashMap的实现,只取用了其中的Key值。

根据这点可以分析出:
1、HashSet支持null值
2、HashSet不支持重复key值
3、HashSet的key会通过hashcode()方法的返回值与hash桶的数组长度取模来决定存放位置
4、HashSet的key值同样会发生hash碰撞

一、HashSet对于Integer包装类型有序

原因:

由于HashSet是根据hashcode值来进行排序的,而Integer类的hashCode方法是直接返回value值,所以直接根据Integer的值来进行大小排序
Integer

二、对于引用类型,如何进行顺序输出

1、对于引用类型想要实现顺序输出,需要通过重写hashcode方法返回一个int类型的属性来实现,参考了Integer类型的内部实现。

class Temp {
    int order;

    public Temp(int order) {
        this.order = order;
    }

    @Override
    public String toString() {
        return "temp:\t"+this.hashCode() +"order:\t"+order;
    }

    @Override
    public int hashCode() {
       return order;
    }
}

2、考虑到HashMap是根据hashcode值和Entry数组长度取模分配存储位置的,只要一直进行扩容处理,在添加元素以及rehash的过程中就不会把元素分配到同一hash桶中。

// 插入元素,HashSet只使用了其中的key值
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
            // 在HashMap中,hash计算是会去取低位再进行按位与来分桶,和取模的方法都是可行的,只是效率不同
            // 如何取低位,按位求与,再进行计算,这里就不再赘述。
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
                // 已经树化直接插入
            else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {
            	// 如果超过树化阈值,转换成红黑树
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        // 大于扩展阈值,进行rehash
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

这里附上hash算法详解的链接
https://blog.csdn.net/a314774167/article/details/100110216

rehash就不贴出源码了,大致就是根据扩容阈值,容量重新计算扩容后的容量,并重新把元素放置到扩容之后的hash桶中,
容量要求2的幂,为了用于算hash值。

顺序输出实现如下:

public class Test {
    public static void main(String[] args) throws InterruptedException {
        Random random = new Random();
       // 这里扩容因子最大可以支持0.187f 原因目前没有结论 后续补充
        HashSet<Temp> hashSet = new HashSet<>(10,0.125f);

        for (int i = 0;i<10000;i++)
        hashSet.add(new Temp(random.nextInt(100000)));
        hashSet.forEach(System.out::println);
    }
}
class Temp {
    int order;

    public Temp(int order) {
        this.order = order;
    }

    @Override
    public String toString() {
        return "temp:\t"+this.hashCode() +"order:\t"+order;
    }

    @Override
    public int hashCode() {
       return order;
    }
}

输出样例

temp:	99026order:	99026
temp:	99031order:	99031
temp:	99030order:	99030
temp:	99033order:	99033
temp:	99048order:	99048
temp:	99051order:	99051
temp:	99053order:	99053
temp:	99071order:	99071
temp:	99079order:	99079
temp:	99078order:	99078
temp:	99086order:	99086
temp:	99135order:	99135
temp:	99138order:	99138
temp:	99150order:	99150
temp:	99156order:	99156
temp:	99173order:	99173
temp:	99177order:	99177
temp:	99185order:	99185
temp:	99187order:	99187
temp:	99189order:	99189
temp:	99218order:	99218
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值