Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.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_Count_Hashmap - Fatal编程技术网

计算java中hashmap中每个值对应的条目数的逻辑

计算java中hashmap中每个值对应的条目数的逻辑,java,count,hashmap,Java,Count,Hashmap,目前,我已经在values字段中对Hashmap进行了排序。 我需要计算hashmap中与每个值关联的条目数 起初,我想迭代排序后的hashmap并计算条目数,直到值不变。为了获得下一个值,我需要进入下一个条目,但是在一个循环迭代结束之前,没有办法这样做。 但我只是迷失在逻辑中,无法继续( 我尝试了另一种在stream()中使用过滤器的逻辑。 对1到50的值应用过滤器,然后计算满足谓词的条目 for(int i = 1; i < COUNT; i++){ int c

目前,我已经在values字段中对Hashmap进行了排序。 我需要计算hashmap中与每个值关联的条目数

起初,我想迭代排序后的hashmap并计算条目数,直到值不变。为了获得下一个值,我需要进入下一个条目,但是在一个循环迭代结束之前,没有办法这样做。 但我只是迷失在逻辑中,无法继续(

我尝试了另一种在stream()中使用过滤器的逻辑。 对1到50的值应用过滤器,然后计算满足谓词的条目

for(int i = 1; i < COUNT; i++){
            int count = (int) single_count.entrySet().stream().filter(v -> v.getValue() == 1).count(); //collect(Collectors.toList());
            unigram_Nc.put(i, count);
        }
for(int i=1;iv.getValue()==1.count();//collect(Collectors.toList());
单格数字输入(i,计数);
}
在本例中,我知道hashmap中的值,但我想知道一个通用解决方案,它返回hashmap中与每个值对应的条目数。
有没有其他方法可以在不知道当前值的情况下计算具有特定值的条目数?

使用java 8 stream api可以更轻松地完成这项工作

为此,您应该从映射中获取值:
map.values()

使用
.stream()
可以获得此集合的流

然后您可以将
collect
方法与
groupingBy
收集器一起使用

最后,它可能看起来像这样:

final Map<Integer, Long> counts = map.values() // get the values
    .stream()                                  // get the stream
    .collect(
        Collectors.groupingBy(                 // the result should be grouped
            o -> o,                            // the key of the result map is just the value
            Collectors.counting()              // the value of result map is the count
        )
    );
Create a class and override its equals() and hashcode() method. Create a field of your preferred type say A and add another int type field to count.
1) Your hashcode() method should return the hash value of your field A.
2) In your equals() method, increase the value of count by 1 and set the count as value

Now create 2 hashmaps, first will have your initial map's value as keys. The second one will have the result of all the counts of values.
final Map counts=Map.values()//获取值
.stream()//获取流
.收集(
Collectors.groupingBy(//应该对结果进行分组
o->o,//结果映射的键就是值
Collectors.counting()//结果映射的值是计数
)
);

对于较旧的JDK,可以这样计算:

final Map<Integer, Long> counts = map.values() // get the values
    .stream()                                  // get the stream
    .collect(
        Collectors.groupingBy(                 // the result should be grouped
            o -> o,                            // the key of the result map is just the value
            Collectors.counting()              // the value of result map is the count
        )
    );
Create a class and override its equals() and hashcode() method. Create a field of your preferred type say A and add another int type field to count.
1) Your hashcode() method should return the hash value of your field A.
2) In your equals() method, increase the value of count by 1 and set the count as value

Now create 2 hashmaps, first will have your initial map's value as keys. The second one will have the result of all the counts of values.
请参阅以下代码段:

class A
{
   Integer count = 1;
   String name;

   @override
   public int hashcode()
   {
      return name.hash();
   }

   @override
   public boolean equals(Object obj)
   {
      obj.count++;   
      secondmap.put(obj.name, obj.count);         

      return true;
   }

}

Now in your main class:

static firstmap = new ConcurrentMap<A, Integer>();

static secondmap = new ConcurrentMap<String, Integer>();

iterate over yourmap
{
   put the value in firstmap as firstmap.put(yourmap value, 0);
}
A类
{
整数计数=1;
字符串名;
@凌驾
公共int hashcode()
{
返回name.hash();
}
@凌驾
公共布尔等于(对象obj)
{
obj.count++;
secondmap.put(对象名称、对象计数);
返回true;
}
}
现在在你的主课上:
静态firstmap=新的ConcurrentMap();
静态secondmap=新的ConcurrentMap();
在地图上迭代
{
将firstmap中的值作为firstmap.put(yourmap值,0);
}
在迭代结束时,您将拥有secondmap中的所有值计数。 注意:如果您的初始映射具有不同的签名,那么您可以通过A的构造函数显式设置字符串名称的值

这只是一个示例,实际实现可能会因您的解决方案而有所不同,但是您可以参考此逻辑。在创建初始映射时,您也可以实现此逻辑。这将为您省去再次迭代的麻烦。

尝试此简单逻辑

Map<String,Integer> dataMap=new LinkedHashMap<String,Integer>(); //Your data map
    Map<Integer,Integer> countMap=new LinkedHashMap<Integer,Integer>(); //map to count data map entries

    //initializing with default value
    dataMap.put("a", 1);
    dataMap.put("b", 2);
    dataMap.put("c", 1);
    dataMap.put("d", 2);
    dataMap.put("e", 1);
    dataMap.put("f", 3);
    dataMap.put("g", 1);

    //main logic
    dataMap.forEach( (k,v) -> {
        if(countMap.get(v)==null)
            countMap.put(v, 0);
        Integer count=countMap.get(v);
        countMap.put(v,count+1);
    } );

    //printing the count
    countMap.forEach( (k,v) -> {
        System.out.println(k+"       "+v);
    } );
Map dataMap=newlinkedhashmap();//您的数据映射
Map countMap=new LinkedHashMap();//计算数据映射项的映射
//使用默认值初始化
dataMap.put(“a”,1);
dataMap.put(“b”,2);
dataMap.put(“c”,1);
dataMap.put(“d”,2);
dataMap.put(“e”,1);
dataMap.put(“f”,3);
dataMap.put(“g”,1);
//主要逻辑
dataMap.forEach((k,v)->{
if(countMap.get(v)=null)
countMap.put(v,0);
整数计数=countMap.get(v);
countMap.put(v,count+1);
} );
//打印计数
countMap.forEach((k,v)->{
系统输出打印项次(k+“”+v);
} );

提示:您需要创建一个新的
映射,其中新映射中的键是来自第一个映射的值,而新映射中的值是条目的计数。谢谢。但是在这种情况下,collect如何在不指定收集器的情况下返回映射。toMap()?另外,将结果类型转换为HashMap是否安全?正如函数的JavaDoc所说:“生成的收集器生成一个映射。”您不能简单地从
Map
转换为
HashMap
,但您可以使用适当的构造函数从
Map
创建一个
HashMap
通过使用三个参数版本:
HashMap counts=map.values().stream().collect(Collectors.groupingBy(o->o,HashMap::new,Collectors.counting())
可以获得
收集器.groupingBy(…)
的返回类型为
HashMap