如何从Hashmap Java

时间:2016-07-19 10:48:11

标签: java count hashmap

我有这个当前的输出:

First clusters: [[1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 1, 1]]  

0 : 4, Probability: 0.8 , logVal: -0.2575424759098898
1 : 1, Probability: 0.2 , logVal: -0.46438561897747244

0 : 3, Probability: 0.6 , logVal: -0.44217935649972373
1 : 2, Probability: 0.4 , logVal: -0.5287712379549449

0 : 5, Probability: 1.0 , logVal: 0.0

0 : 4, Probability: 0.8 , logVal: -0.2575424759098898
1 : 1, Probability: 0.2 , logVal: -0.46438561897747244

0 : 3, Probability: 0.6 , logVal: -0.44217935649972373
1 : 2, Probability: 0.4 , logVal: -0.5287712379549449

然后对于每个logVal我想总结它们为例:
第一个数据:

0 : 4, Probability: 0.8 , logVal: -0.2575424759098898  
1 : 1, Probability: 0.2 , logVal: -0.46438561897747244  

total[0] = (-0.2575424759098898) + (-0.46438561897747244)

接下来,第二个数据:

0 : 3, Probability: 0.6 , logVal: -0.44217935649972373  
1 : 2, Probability: 0.4 , logVal: -0.5287712379549449

total[1] = (-0.44217935649972373) + (-0.5287712379549449)

等等,直到最后一个数据。但是我不确定如何获取每个数据的索引,因此每个数据logVal可以使用HashMap一起完成。

到目前为止,这是我的代码:

public static void printCount(HashMap<String, Integer> countMap, int length) {
    double logVal = 0.0, entropy = 0.0, probability = 0.0;

    Set<String> keySet = countMap.keySet();

    for (String bit : keySet) {
        //System.out.println(bit + " : " + countMap.get(bit));

        probability = (double)countMap.get(bit)/length;

        logVal = (probability*(Math.log(probability)/Math.log(2)));

        System.out.println(bit + " : " + countMap.get(bit) + ", Probability: " + probability + " , logVal: " + logVal);

        total = logVal + logVal;

        System.out.println("Total = " + total);

    }

    System.out.println();

}

1 个答案:

答案 0 :(得分:0)

变化:

total = logVal + logVal;
System.out.println("Total = " + total);

通过

String totalString = "(" + logVal + ") + (" + logVal ")";
System.out.println("Total = " + totalString);

如果您需要存储它(您写了total[0] = ...,只需使用String[] totals