Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hashmap无法统计所有发生的事件_Java_Hashmap_Reduce - Fatal编程技术网

Java Hashmap无法统计所有发生的事件

Java Hashmap无法统计所有发生的事件,java,hashmap,reduce,Java,Hashmap,Reduce,Hashmap无法统计所有发生的事件 这是来自Hadoop Reduce()的示例代码 我的mapper out键和值是 key---------6.9 v---------------Iris-versicolor v---------------Iris-virginica v---------------Iris-virginica v---------------Iris-versicolor v---------------Iris-virginica v---------------

Hashmap无法统计所有发生的事件

这是来自Hadoop Reduce()的示例代码

我的mapper out键和值是

key---------6.9
v---------------Iris-versicolor
v---------------Iris-virginica
v---------------Iris-virginica
v---------------Iris-versicolor
v---------------Iris-virginica
v---------------Iris-virginica
v---------------Iris-virginica
v---------------Iris-virginica
v---------------Iris-virginica
v---------------Iris-virginica
v---------------Iris-versicolor
v---------------Iris-virginica
v---------------Iris-versicolor
v---------------Iris-versicolor
v---------------Iris-virginica
我的预期结果是

getVal-----------{Iris-versicolor=5, Iris-virginica=10}
但我得到的输出是

getVal-----------{Iris-versicolor=1, Iris-virginica=2}
我所做的就是

System.out.println("key---------"+key);
for(Text val:values){
    String v = val.toString();
    System.out.println("v---------------"+v);
    if (getVal.isEmpty()) {
        getVal.put(v, 1);
    } 
    else {
        for (String colId : getVal.keySet()) {
        if(colId.equals(v)){
            Integer val1 = getVal.get(colId);
            val1 = val1 + 1;
            getVal.put(colId, val1);
        }
        else{
            getVal.put(v, 1);
        }

    }

    }
}
System.out.println("getVal-----------"+getVal);
我做错什么了吗

  • 删除else子句
  • 如果地图不存在,则向地图添加值-地图为空时不添加值 例如:

    System.out.println( "key---------" + key );
    
    for ( String value : values ) {
    
      // I used `String` instead of your `Text` type
      // so I do not need this line
      // String v = value.toString();
    
      System.out.println( "v---------------" + value );
    
      // if the value is not present, add it!
      // each value will be added once!
      if ( !valueCounterMap.containsKey( value ) )
        valueCounterMap.put( value, 1 );
    
      // if the value is already present, just 
      // increment its counter by 1
      else {
    
          Integer counter = valueCounterMap.get( value );
          valueCounterMap.put( value, ++counter );
      }
    }
    
    System.out.println( "getVal-----------" + valueCounterMap );
    
    结果:

    getVal-----------{Iris-versicolor=5, Iris-virginica=10}
    

    不,没有空格。请发表评论,以便我可以改进问题