2012-07-26 30 views
0

我發現這個問題很有趣。我正在使用一個awk二維數組,它具有相同的鍵,值和數量。並正在打印到一個文件。該文件是在下面的格式用java計算awk二維數組轉換成hashmap

A100|B100|3 
A100|C100|2 
A100|B100|5 

現在我有一個這樣的文件。我的動機是這個文件轉換成一個哈希表,以便從散列圖最終輸出的是。

A100|B100|8 
A100|C100|2 

只是一個聚集

面臨的挑戰是,這其中有3個維度和而不是兩個。我確實有在下面的格式是

D100|4 
H100|5 
D100|6 

我容易凝集上面,因爲它是唯一的2D和我用下面的代碼做

String[] fields= strLine.trim().split("\\|"); 
if(hashmap.containsKey(fields[0])) 
{ 
//Update the value of the key here 
hashmap.put(fields[0],hashmap.get(fields[0]) + Integer.parseInt(fields[1])); 
} 
else 
{ 
//Inserting the key to the map 
hashmap.put(fields[0],Integer.parseInt(fields[1])); 
} 

因此,這是很簡單的另一個文件實施。

但是,當涉及到3D我必須有內部的另一個檢查。我的這種想法是保持一個[B100,5(beanObject [5])

Map<String,beanClassObject> hashmap=new Map<String,beanClassObject>(); 

secongField哈希地圖,已經在創建的本體Object腳本和密鑰之間具有映射關係的代碼中用作第二個字段「例如它是」

此Bean類將具有第二個和第三個字段的getter和setter方法的文件。我希望我清楚這一點。所以這個實施將會是

if(hashmap.containsKey(fields[0])) 
{ 
    **//Have to check whether the the particular key value pair already exists ... I dint find any method for this ... Just a normal iteration is there .. Could you ppl guide me regarding this** 

    //Update the value of the key here 
    secondFieldHashMap.get(fields[1]).COunt= secondFieldHashMap.get(fields[1]).getCOunt+ Integer.parseInt(fields[2])); 
    } 
    else 
    { 
    //Inserting the key to the map 
    hashmap.put(fields[0],Integer.parseInt(fields[1])); 
    secondFieldHashMap.get(fields[1]).COunt= Integer.parseInt(fields[2])); 
    } 
else 
{ 

// This meands there is no key field 
// Hence insert the key field and also update the count of seconfFieldHashMap as done previously. 
} 

教你體育,請提出一些想法。謝謝

回答

0

考慮使用Google Guava庫中的Table

+0

謝謝你分享相同..我不確定爲了使用它而必須包含的庫。你能否指導我一樣 – NandaKumar 2012-07-26 20:06:24