如何通过Java中的键对Map值进行排序? [英] How to sort Map values by key in Java?

查看:438
本文介绍了如何通过Java中的键对Map值进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含键和值的字符串的Map。



数据如下:


question1,1
question9,1

question2,4

question5,2

我想根据键对地图进行排序。所以,最后,我会有 question1,question2,question3 ....等等。



最终,我试图从这张地图中取出两个字符串。



  • 第一个字符串:问题(按顺序1 ..10)第二个字符串:Answers(按照与问题相同的顺序)



现在我拥有以下内容:

  Iterator it = paramMap.entrySet()。iterator(); (it.hasNext()){
Map.Entry pairs =(Map.Entry)it.next();
questionAnswers + = pairs.getKey()+,;
}

这让我得到了字符串中的问题,但它们并没有按顺序排列。 / b>

解决方案

简短回答



使用 TreeMap 。这正是它的目的。



如果将此地图传递给您并且您无法确定类型,则可以执行以下操作:

 的SortedSet<字符串> keys = new TreeSet<>(map.keySet()); 
for(String key:keys){
String value = map.get(key);
//做某事
}

这将在自然中迭代整个地图



更长的答案

从技术上讲,你可以使用任何实现 SortedMap 的东西,但除了极少数情况下,这相当于 TreeMap ,就像使用 Map 实现通常等于 HashMap

如果你的键是一个复杂的类型,它没有实现Comparable,或者你不想使用自然顺序,那么 TreeMap TreeSet 有额外的构造函数,可以让你传入 Comparator

  //作为演示内联放置,但不一定是lambda表达式
比较器< Foo>比较器=(Foo o1,Foo o2) - > {
...
}

SortedSet< Foo> keys = new TreeSet<>(比较器);
keys.addAll(map.keySet());

记住使用 TreeMap TreeSet ,它将具有与 HashMap HashSet 不同的性能特性。粗略地说,查找或插入元素的操作将从 O(1) O(Log(N))

HashMap 中,从1000个项目移动到10,000个并不会真正影响查找元素的时间,而是对于 TreeMap 查找时间将减慢大约3倍(假设Log 2 )。对于每个元素查找,从1000移动到100,000将会慢6倍。


I have a Map that has strings for both keys and values.

Data is like following:

"question1", "1"
"question9", "1"
"question2", "4"
"question5", "2"

I want to sort the map based on its keys. So, in the end, I will have question1, question2, question3....and so on.


Eventually, I am trying to get two strings out of this Map.

  • First String: Questions ( in order 1 ..10)
  • Second String: Answers (in the same order as the question)

Right now I have the following:

Iterator it = paramMap.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry) it.next();
    questionAnswers += pairs.getKey() + ",";
}

This gets me the questions in a string but they are not in order.

解决方案

Short answer

Use a TreeMap. This is precisely what it's for.

If this map is passed to you and you cannot determine the type, then you can do the following:

SortedSet<String> keys = new TreeSet<>(map.keySet());
for (String key : keys) { 
   String value = map.get(key);
   // do something
}

This will iterate across the map in natural order of the keys.


Longer answer

Technically, you can use anything that implements SortedMap, but except for rare cases this amounts to TreeMap, just as using a Map implementation typically amounts to HashMap.

For cases where your keys are a complex type that doesn't implement Comparable or you don't want to use the natural order then TreeMap and TreeSet have additional constructors that let you pass in a Comparator:

// placed inline for the demonstration, but doesn't have to be a lambda expression
Comparator<Foo> comparator = (Foo o1, Foo o2) -> {
        ...
    }

SortedSet<Foo> keys = new TreeSet<>(comparator);
keys.addAll(map.keySet());

Remember when using a TreeMap or TreeSet that it will have different performance characteristics than HashMap or HashSet. Roughly speaking operations that find or insert an element will go from O(1) to O(Log(N)).

In a HashMap, moving from 1000 items to 10,000 doesn't really affect your time to lookup an element, but for a TreeMap the lookup time will be about 3 times slower (assuming Log2). Moving from 1000 to 100,000 will be about 6 times slower for every element lookup.

这篇关于如何通过Java中的键对Map值进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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