使用TreeMap和Comparator通过值对HashMap进行排序 [英] Sorting HashMap by value using a TreeMap and Comparator

查看:170
本文介绍了使用TreeMap和Comparator通过值对HashMap进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Im使用以下代码创建一个hashmap,然后使用树形图和比较器对hashmap中的值进行排序。但是,输出是意想不到的。

 














public static void main(String [] args){
System.out.println(Most freq+ mostFreq(i me hello hello hello me));
}


public static String [] mostFreq(String str){

if((str == null)|| ().equalsIgnoreCase()))
return null;

String [] arr = new String [10];

String [] words = str.split();

映射< String,Integer> map = new HashMap< String,Integer>();

for(String word:words)
{
int count = 0;
if(map.containsKey(word))
{
count = map.get(word);
map.put(word,count + 1);
}
else
map.put(word,1);
}

MyComparator comp = new MyComparator(map);
Map< String,Integer> newMap = new TreeMap(comp);
newMap.putAll(map);
Iterator it = newMap.entrySet()。iterator();
while(it.hasNext())
{
Map.Entry pairs =(Map.Entry)it.next();
System.out.println(Key+ pairs.getKey()+ - value+ pairs.getValue());
}

return arr;
}

这里是比较

  package samplecodes; 

import java.util.Comparator;
import java.util.Map;

public class MyComparator implements Comparator {

映射地图;

public MyComparator(Map map){
this.map = map;
}

@Override
public int compare(Object o1,Object o2){
return((Integer)map.get(o1)> map.get(o2)?(Integer)map.get(o1):( Integer)map.get(o2));
}

}

  me-2 
hello-3
i-3


解决方案

请检查 比较 :您不返回较大的值,但 o1 < code> -1 o2 0 for o1 = o1 > o2 1 c $ c>。所以你可以写:

  @Override 
public int compare(Object o1,Object o2){
return((Integer)map.get(o1))。compareTo((Integer)map.get(o2);
}


Im using the following code to create a hashmap and then sort the values in the hashmap by using a treemap and a comparator. However, the output is rather unexpected. So any thoughts as to what Im doing wrong would be helpful

Code

public static void main(String[] args) {
    System.out.println("Most freq"+mostFreq(" i me hello hello hello me"));
}


public static String[] mostFreq(String str){

    if ((str==null)||( str.trim().equalsIgnoreCase("")))
        return null;

    String[] arr = new String[10];

    String[] words= str.split(" ");

    Map <String,Integer> map = new HashMap<String,Integer>();

    for (String word :words)
    { 
        int count =0;
        if (map.containsKey(word))
        {     
            count= map.get(word);
            map.put(word, count+1);
        }             
        else
            map.put(word, 1);
    }

    MyComparator comp= new MyComparator(map);
    Map<String,Integer> newMap= new TreeMap(comp);
    newMap.putAll(map);
    Iterator it= newMap.entrySet().iterator();
    while (it.hasNext())
    {
        Map.Entry pairs = (Map.Entry) it.next();
        System.out.println("Key  "+pairs.getKey()+"-- value"+pairs.getValue());
    }

    return arr;
}

Here's the comparator

package samplecodes;

import java.util.Comparator;
import java.util.Map;

public class MyComparator implements Comparator {

    Map map;

    public MyComparator(Map map){
        this.map=map;
    }

    @Override
    public int compare(Object o1, Object o2) {
        return ((Integer)map.get(o1) >(Integer)map.get(o2)? (Integer)map.get(o1):(Integer)map.get(o2));
    }

}

And the output is of the form

me-2
hello-3
i-3

解决方案

Please check the JavaDoc of compare: You do not return the bigger value, but -1 for o1 < o2, 0 for o1 = o2 and 1 for o1 > o2. So you could write:

@Override
public int compare(Object o1, Object o2) {
    return ((Integer) map.get(o1)).compareTo((Integer) map.get(o2);
}

这篇关于使用TreeMap和Comparator通过值对HashMap进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆