在Java中,通过key.length()对哈希映射进行排序 [英] In Java, sort hash map by its key.length()

查看:162
本文介绍了在Java中,通过key.length()对哈希映射进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  HashMap< String,Integer> map = new HashMap< String,Integer>(); 
map.put(java,4);
map.put(go,2);
map.put(objective-c,11);
map.put(c#,2);

现在我想按照它的密钥长度对这张地图进行排序,如果两个密钥长度相等和c#都是长度2),然后按alphba顺序排序。
,所以我期望得到的结果是这样的:

打印结果:
objective-c,11
java,4
c#,2
go,2



这是我自己的attamp,但它根本不起作用...

  HashMap< String,Integer> map = new HashMap< String,Integer>(); 
map.put(java,4);
map.put(go,2);
map.put(objective-c,11);
map.put(c#,2);

地图< String,Integer> treeMap = new TreeMap< String,Integer>(
new Comparator< String>(){
@Override
public int compare(String s1,String s2){
return s1。 length()。compareTo(s2.length());
}
}
);

其实'compareTo'方法显示为红色(不能编译)....
请别人帮助我一些代码示例...我有点困惑于如何使用比较器类来自定义比较对象...

解决方案

编译器抱怨,因为你不能在 int 上调用 compareTo 。排序地图的正确方法如下:

  Map< String,Integer> treeMap = new TreeMap< String,Integer>(
new Comparator< String>(){
@Override
public int compare(String s1,String s2){
if(s1 .length()> s2.length()){
return -1;
} else if(s1.length() return 1;
} else {
return s1.compareTo(s2);
}
}
});

前两个条件比较两个 String s并相应地返回正数或负数。第三个条件是按照字典顺序比较字符串的长度是否相等。


i have a hashmap like this:

HashMap<String,Integer> map = new HashMap<String,Integer>();
map.put("java",4);
map.put("go",2);
map.put("objective-c",11);
map.put("c#",2);

now i want to sort this map by its key length, if two keys length are equal (e.g go and c# both length 2), then sorted by alphba order. so the outcome i expect to get is something like:

printed result: objective-c, 11 java, 4 c#, 2 go, 2

here is my own attamp, but it doesnt work at all...

      HashMap<String,Integer> map = new HashMap<String,Integer>();
          map.put("java",4);
          map.put("go",2);
          map.put("objective-c",11);
          map.put("c#",2);

      Map<String,Integer> treeMap = new TreeMap<String, Integer>(
                new Comparator<String>() {
                    @Override
                    public int compare(String s1, String s2) {
                        return s1.length().compareTo(s2.length());
                    }
                }
        );

actually the 'compareTo' method appears as red (not be able to compile).... please someone help me with some code example...i am a bit confusing with how to use comparator class to customize compare object...

解决方案

The compiler is complaining because you cannot call compareTo on an int. The correct way to sort the map is the following:

Map<String, Integer> treeMap = new TreeMap<String, Integer>(
    new Comparator<String>() {
        @Override
        public int compare(String s1, String s2) {
            if (s1.length() > s2.length()) {
                return -1;
            } else if (s1.length() < s2.length()) {
                return 1;
            } else {
                return s1.compareTo(s2);
            }
        }
});

The first two conditions compare the lengths of the two Strings and return a positive or a negative number accordingly. The third condition would compare the Strings lexicographically if their lengths are equal.

这篇关于在Java中,通过key.length()对哈希映射进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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