HashSet的最大大小 [英] Maximum Size of HashSet

查看:187
本文介绍了HashSet的最大大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我生成随机的10000个ip地址,我想存储在HashSet中找到的所有那些ip地址,但是根据我的计算,找到了大约6000个ip地址但是在HashSet中只有700个ip地址被存储了?在存储String方面HashSet是否有任何限制。任何建议将不胜感激。

So Basically I am generating random 10000 ip address and I wanted to store all those ip address that are found in HashSet but according to my calculation around 6000 ip address were found but in HashSet only 700 ip address are getting stored? Is there any limitations in HashSet in terms of storing String. Any suggestions will be appreciated.

   Set<String> ipFine = new HashSet<String>();
        long runs = 10000;
            while(runs > 0) {

            String ipAddress = generateIPAddress();

            resp = SomeClass.getLocationByIp(ipAddress);

            if(resp.getLocation() != null) {

                 ipFine.add(ipAddress);
                        }

               runs--;

         }


推荐答案

目前为止如您所关注的,没有限制(限制是数组的最大大小,即2 ** 31)。

As far as you're concerned, there is no limit (the limit is the max size of an array, which is 2**31).

然而,设置仅存储唯一值,因此我的猜测是您只生成了700个唯一地址。

However, Sets only store unique values, so my guess is that you only generated 700 unique addresses.

修改你的代码如下:

if(resp.getLocation() != null) {
    if (ipFine.add(ipAddress)) { // add() returns true if the value is unique
        runs--; // only decrement runs if it's a new value
    }
}

这个修改意味着您将继续循环,直到获得10000 唯一值。

This modification will mean you'll keep looping until you get 10000 unique values.

这篇关于HashSet的最大大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆