如何从HashMAP中获取相同的值?
HashMap<HashMap<String, Float>, String> HM=new HashMap<HashMap<String,Float>, String>();
HashMap<String, Float> h;
h=new HashMap<String, Float>();
h.put("X", 48.0f);
h.put("Y", 80.0f);
HM.put(typeValuesHM, "Red");
h=new HashMap<String, Float>();
h.put("X", 192.0f);
h.put("Y", 80.0f);
HM.put(typeValuesHM, "Red");
h=new HashMap<String, Float>();
h.put("X", 192.0f);
h.put("Y", 320.0f);
HM.put(typeValuesHM, "Blue");
h=new HashMap<String, Float>();
h.put("X", 336.0f);
h.put("Y", 560.0f);
HM.put(typeValuesHM, "Blue");
我的HashMap HM的值如下:
{ {x=48,y=80}=Red,{x=192,y=80}=Red,{x=192,y=320}=Blue,{x=336,y=560}=Blue }
这里,
我想统计HashMap HM中的相似值。
即)如果我给值等于“红色”意味着我想得到 count=2。 如果我给值等于“蓝色”意味着我想得到 count=2。
如何从HashMAP HM中得到相同的值?
最佳答案
int count = Collections.frequency(new ArrayList<String>(HM.values()), "Red");
关于java - 如何从 HashMap 中获取相同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5117357/