在java中将带有计数的awk 2D数组转换为hashmap
我发现这个问题很有趣。我使用的是一个具有相同键、值和计数的awk 2D数组。而这正被打印到一个文件中。此文件的格式如下在java中将带有计数的awk 2D数组转换为hashmap,java,awk,hashmap,Java,Awk,Hashmap,我发现这个问题很有趣。我使用的是一个具有相同键、值和计数的awk 2D数组。而这正被打印到一个文件中。此文件的格式如下 A100|B100|3 A100|C100|2 A100|B100|5 现在我有一个这样的文件。。我的动机是将此文件转换为哈希映射,以便哈希映射的最终输出为 A100|B100|8 A100|C100|2 只是一个集合 挑战是,这个有三个维度,而不是两个维度。我确实有一个以下格式的另一个文件 D100|4 H100|5 D100|6 我很容易地聚合了上面的内容,因为它只是
A100|B100|3
A100|C100|2
A100|B100|5
现在我有一个这样的文件。。我的动机是将此文件转换为哈希映射,以便哈希映射的最终输出为
A100|B100|8
A100|C100|2
只是一个集合
挑战是,这个有三个维度,而不是两个维度。我确实有一个以下格式的另一个文件
D100|4
H100|5
D100|6
我很容易地聚合了上面的内容,因为它只是二维的,我使用了下面的代码来做到这一点
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>();
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.
}