我正在开发一个项目,最终得到了一个很大的 HashMap ,现在我正在尝试按值对其进行排序。
ArrayList<Map.Entry<String, Integer>> entries = new ArrayList<Map.Entry<String, Integer>>(BloomFilter.map.entrySet());
但是当我这样做时,我收到 OutOfMemoryError。
有什么办法可以防止这种情况发生吗?
编辑:如果它在bloomfilter函数中,这就是我的
hash1 = MurmurHash2.hash32(genom);
hash2 = genom.hashCode();
inList = true;
for (int i = 0; i < k-1 ; i++) {
hashedGenom = ( hash1 + hash2 * i) % a.size();
hashedGenom = CheckForNegative(hashedGenom);
if(!(a.get(hashedGenom))){
a.set(hashedGenom);
inList = false;
}
}
return inList;
这是我做布隆过滤器的地方:
if(CheckIfThere(s, k, fBitset)){
// System.out.println("var");
val = map.get(s);
if(val != null){
map.put(s, map.get(s) + 1);
//map.remove(s);
//map.put(s, new Integer(val + 1));
}else{
map.put(s,1);
}
我基本上是获取字符串并将其发送到 CheckIfThere,如果它为 true,我会将其放入 hashmap。
最佳答案
有几种算法可以解决它
但我认为最简单的方法是使用数据库。
您可以将所有值插入 mysql/oracle/sql server/postgres ...然后
select xxxx from xxx order by xxx
如果你担心数据库太大,部署困难,可以试试sqlite
关于java - 如何以内存有效的方式对大型 HashMap 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41309388/