Java 如何根据其值按升序打印出 HashMap<String, String> 的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3605237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:32:01  来源:igfitidea点击:

How print out the contents of a HashMap<String, String> in ascending order based on its values?

javasortinghashmap

提问by Carlsberg

I have this HashMapthat I need to print out in ascending order according to the valuescontained in it (not the keys).

我有这个HashMap,我需要根据其中包含的而不是键)按升序打印出来。

But the order when I print it out is seemingly random.

但是我打印出来的顺序似乎是随机的。

What's the best way to print it out in ascending value order?

按升序打印出来的最佳方法是什么?

Map<String, String> codes = new HashMap<String, String>();

codes.put("A1", "Aania");
codes.put("X1", "Abatha");
codes.put("C1", "Acathan");
codes.put("S1", "Adreenas");

In other words, the example above should print out as this:

换句话说,上面的例子应该打印如下:

A1, Aania
X1, Abatha
C1, Acathan
S1, Adreenas

采纳答案by matt b

You aren't going to be able to do this from the HashMap class alone.

您将无法仅从 HashMap 类中执行此操作。

I would take the Map<String, String> codes, construct a reverse map of TreeMap<String, String> reversedMapwhere you map the values of the codesMap to the keys (this would require your original Map to have a one-to-one mapping from key-to-value). Since the TreeMap provides Iterators which returns entries in ascending key order, this will give you the value/key combination of the first map in the order (sorted by values) you desire.

我会使用Map<String, String> codes, 构造一个反向映射,TreeMap<String, String> reversedMapcodesMap的值映射到键的位置(这将要求您的原始 Map 具有从键到值的一对一映射)。由于 TreeMap 提供了按键升序返回条目的迭代器,这将为您提供按所需顺序(按值排序)的第一个映射的值/键组合。

Map<String, String> reversedMap = new TreeMap<String, String>(codes);

//then you just access the reversedMap however you like...
for (Map.Entry entry : reversedMap.entrySet()) {
    System.out.println(entry.getKey() + ", " + entry.getValue());
}

There are several collections libraries (commons-collections, Google Collections, etc) which have similar bidirectional Map implementations.

有几个集合库(commons-collections、Google Collections 等)具有类似的双向 Map 实现。

回答by bmargulies

  1. Create a TreeMap<String,String>
  2. Add each of the HashMapentries with the value as the key.
  3. iterate the TreeMap
  1. 创建一个 TreeMap<String,String>
  2. HashMap以值作为键添加每个条目。
  3. 迭代 TreeMap

If the values are nonunique, you would need a list in the second position.

如果值不唯一,则您需要在第二个位置有一个列表。

回答by Sean

You'll need to make a list of the keys, sort them according to the corresponding values, then iterate over the sorted keys.

您需要制作一个键列表,根据相应的值对它们进行排序,然后遍历排序后的键。

Map<String, String> map = getMyMap();
List<String> keys = new ArrayList<String>(map.keySet());
Collections.sort(keys, someComparator);
for (String key: keys) {
    System.out.println(key + ": " + map.get(key));
}

As for what to use for someComparator, here are some handy, generic Comparator-creating routines I often find useful. The first one sorts by the values according to their natural ordering, and the second allows you to specify any arbitrary Comparator to sort the values:

至于用于什么someComparator,这里有一些我经常发现有用的方便的通用比较器创建例程。第一个根据值的自然顺序对值进行排序,第二个允许您指定任意 Comparator 来对值进行排序:

public static <K, V extends Comparable<? super V>>
        Comparator<K> mapValueComparator(final Map<K, V> map) {
    return new Comparator<K>() {
        public int compare(K key1, K key2) {
            return map.get(key1).compareTo(map.get(key2));
        }
    };
}

public static <K, V>
        Comparator<K> mapValueComparator(final Map<K, V> map,
                                         final Comparator<V> comparator) {
    return new Comparator<K>() {
        public int compare(K key1, K key2) {
            return comparator.compare(map.get(key1), map.get(key2));
        }
    };
}

回答by aar

The simplest solution would be to use a sorted map like TreeMap instead of HashMap. If you do not have control over the map construction, then the minimal solution would be to construct a sorted set of keys. You don't really need a new map.

最简单的解决方案是使用像 TreeMap 这样的排序映射而不是 HashMap。如果您无法控制地图构造,那么最小的解决方案是构造一组排序的键。你真的不需要新地图。

Set<String> sortedKeys = new TreeSet<String>();
sortedKeys.addAll(codes.keySet());

for(String key: sortedKeys){
    println(key  + ":" + codes.get(key));
}

回答by Peter Lawrey

You can use a list of the entry set rather than the key set and it is a more natural choice given you are sorting based on the value. This avoids a lot of unneeded lookups in the sorting and printing of the entries.

您可以使用条目集的列表而不是键集,鉴于您根据值进行排序,这是一个更自然的选择。这避免了在条目的排序和打印中进行大量不必要的查找。

Map<String, String> map = ...
List<Map.Entry<String, String>> listOfEntries = new ArrayList<Map.Entry<String, String>>(map.entrySet());
Collections.sort(listOfEntries, new SortByValueComparator());
for(Map.Entry<String, String> entry: listOfEntries)
   System.out.println(entry);

static class SortByValueComparator implements Comparator<Map.Entry<String, String>> {
   public int compareTo(Map.Entry<String, String> e1, Map.Entry<String, String> e2) {
       return e1.getValue().compateTo(e2.getValue());
   }
}

回答by psun

the for loop of for(Map.Entry entry: codes.entrySet()) didn't work for me. Used Iterator instead.

for(Map.Entry entry:) 的 for 循环codes.entrySet()对我不起作用。改为使用迭代器。

Iterator<Map.Entry<String, String>> i = codes.entrySet().iterator(); 
while(i.hasNext()){
    String key = i.next().getKey();
    System.out.println(key+", "+codes.get(key));
}

回答by deeps

Try:

尝试:

try
{
    int cnt= m.getSmartPhoneCount("HTC",true);      
    System.out.println("total count of HTC="+cnt);
}  
catch (NoSuchBrandSmartPhoneAvailableException e)
{
    // TODO Auto-generated catch 
    e.printStackTrace();
}

回答by deeps

 SmartPhone[] sp=new SmartPhone[4];
 sp[0]=new SmartPhone(1,"HTC","desire","black",20000,10,true,true);
 sp[1]=new SmartPhone(2,"samsung","grand","black",5000,10,false,true);
 sp[2]=new SmartPhone(14,"google nexus","desire","black",2000,30,true,false);
 sp[3]=new SmartPhone(13,"HTC","desire","white",50000,40,false,false);

回答by Roks

while (itr.hasNext()) {
    Vehicle vc=(Vehicle) itr.next();
    if(vc.getVehicleType().equalsIgnoreCase(s)) {
        count++;
    }
}

回答by Vladimir

It's time to add some lambdas:

是时候添加一些 lambda 表达式了:

codes.entrySet()
    .stream()
    .sorted(Comparator.comparing(Map.Entry::getValue))
    .forEach(System.out::println);