2016-12-24 27 views
1

我正在一個項目上工作,最後我得到一個大的散列表,現在我試圖按值對它進行排序。如何以內存有效的方式對值進行大型hashmap排序?

ArrayList<Map.Entry<String, Integer>> entries = new ArrayList<Map.Entry<String, Integer>>(BloomFilter.map.entrySet()); 

但是當我這樣做,我得到一個OutOfMemoryError。

有什麼辦法可以防止這種情況發生?

編輯:這是我的,如果是在布隆過濾器功能

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和如果它變成真的,我把它放到hashmap。

+0

多大?你是否給虛擬機一個足夠大的堆? – pvg

+0

'BloomFilter.map.entrySet()'做了什麼? – teppic

+0

Collections.sort(條目,新比較>(){ \t \t \t \t @Override \t \t \t \t公衆詮釋比較(條目<字符串,整數>第一,條目<字符串,整數>第二){ \t \t \t \t \t返回second.getValue() - first.getValue(); \t \t \t \t} \t \t \t}); – bocante

回答

1

有幾種算法來解決它

,但我認爲最簡單的方法是使用數據庫。

您可以將所有的值到MySQL/ORACLE/SQL服務器/ Postgres的...然後

select xxxx from xxx order by xxx 

,如果你擔心數據庫大,很難部署,有sqlite

一試
+0

它是一個控制檯應用程序,我想我不應該使用數據庫 – bocante

+0

請不要把數據庫作爲你認爲的數據庫,sqlite只是一個SQL接口內的**外部排序算法庫**。和**外部排序算法**是什麼,你需要:-) –

相關問題