简体   繁体   English

Java-替换位集

[英]Java - BitSet Replacement

In my code I am trying to get if a number exist in the hashmap or not. 在我的代码中,我试图获取哈希图中是否存在数字。 My code is following: 我的代码如下:

BitSet arp = new BitSet();

for i = 0 to 10 million

HashMap.get (i)

if number exist
arp.set(i , true)

else
arp.set(i , false)

After that from bitset I get if number i exist or not. 之后,从位集中获得数字i是否存在。 However, I found this bitset operation is quite slow (tried with string = string + 0/1 also, more slower). 但是,我发现此位集操作相当慢(也尝试使用string = string + 0/1 ,更慢)。 Can anybody help me how to replace this operation with a faster one. 有人可以帮助我如何以更快的速度替换此操作。

Your code is really difficult to read clearly, but I suspect you're just trying to set bits in the BitSet that are keys from your HashMap ? 您的代码确实很难清晰阅读,但是我怀疑您只是在尝试设置BitSet中的位,这些BitSetHashMap键?

In that case, your code should just be more or less 在这种情况下,您的代码应该或多或少

BitSet bits = new BitSet(10000000);
for (Integer k : map.keySet()) {
  bits.set(k);
}

Even if this wasn't what you meant, as a general rule, BitSet is blazing fast; 即使这不是您的意思,但一般而言, BitSet的速度很快。 I suspect it's the rest of your code that's slow. 我怀疑是其余的代码很慢。

If you provided your actual relevant code, we could have found some performance errors in the first place. 如果您提供了实际的相关代码,那么我们可能首先发现了一些性能错误。 But assuming your code is ok and you profiled your application to make sure that the BitSet operations are actually slow: 但是,假设您的代码还可以,并且您对应用程序进行了配置,以确保BitSet操作实际上很慢:

If you have enough memory space available, you can always just go for a boolean[] instead of a BitSet . 如果您有足够的可用内存空间,则始终可以选择boolean[]而不是BitSet

BitSet internally uses long[] to store the separate bits, so it's very good memory-wise, but can sometimes be a little bit too slow. BitSet内部使用long[]存储单独的位,因此在内存方面非常好,但有时可能会太慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM