简体   繁体   English

基于ArrayList5 Size对ArrayList的HashMap进行排序

[英]Sort HashMap of ArrayList Based on ArrayList5 Size

I realize there are questions similar to this, but they do not answer my question. 我意识到有类似的问题,但他们没有回答我的问题。

I need to return the keys of my HashMap, based on the size of the corresponding value's ArrayList. 我需要根据相应值的ArrayList的大小返回我的HashMap的键。 For example, if I have: 例如,如果我有:

HashMap<String,ArrayList<Integer>> unsortedMap = new HashMap<String,ArrayList<Integer>>();
unsortedMap.put("A",new ArrayList<Integer>(Arrays.asList(1,2,3)));
unsortedMap.put("B",new ArrayList<Integer>(Arrays.asList(4)));
unsortedMap.put("C",new ArrayList<Integer>(Arrays.asList(2,3,1,4)));

I'd like it to return "C" "A" "B" 我想要它返回“C”“A”“B”

For Java 7, you can call entrySet() to get a Set<Map.Entry<String,ArrayList<Integer>>> - which you can then use to populate something like an ArrayList<Map.Entry<String,ArrayList<Integer>>> which you can sort with a custom comparator. 对于Java 7,您可以调用entrySet()来获取Set<Map.Entry<String,ArrayList<Integer>>> - 然后您可以使用它来填充类似ArrayList<Map.Entry<String,ArrayList<Integer>>>您可以使用自定义比较器进行排序。

import java.util.*;

public class Test {
  public static void main(String[] args) {
    Map<String, ArrayList<Integer>> unsortedMap = new HashMap<>();
    unsortedMap.put("A", new ArrayList<Integer>(Arrays.asList(1, 2, 3)));
    unsortedMap.put("B", new ArrayList<Integer>(Arrays.asList(4)));
    unsortedMap.put("C", new ArrayList<Integer>(Arrays.asList(2, 3, 1, 4)));

    List<Map.Entry<String, ArrayList<Integer>>> list = 
        new ArrayList<>(unsortedMap.entrySet());
    Collections.sort(list, new EntryComparator());

    for (Map.Entry<String, ArrayList<Integer>> entry : list) {
      System.out.println(entry.getKey());
    }
  }

  private static class EntryComparator
      implements Comparator<Map.Entry<String, ArrayList<Integer>>>
  {
    public int compare(Map.Entry<String, ArrayList<Integer>> left,
        Map.Entry<String, ArrayList<Integer>> right) {     
      // Right then left to get a descending order
      return Integer.compare(right.getValue().size(), left.getValue().size());
    }
  }
}

In Java 8 you can use the streams API to make it slightly more fluent - while taking basically the same steps. 在Java 8中,您可以使用流API使其更加流畅 - 同时采取基本相同的步骤。

import java.util.*;
import java.util.stream.*;

public class Test {
  public static void main(String[] args) {
    Map<String, ArrayList<Integer>> unsortedMap = new HashMap<>();
    unsortedMap.put("A", new ArrayList<Integer>(Arrays.asList(1, 2, 3)));
    unsortedMap.put("B", new ArrayList<Integer>(Arrays.asList(4)));
    unsortedMap.put("C", new ArrayList<Integer>(Arrays.asList(2, 3, 1, 4)));

    List<String> keys = unsortedMap
          .entrySet()
          .stream()
          .sorted((left, right) ->
              Integer.compare(right.getValue().size(), left.getValue().size()))
          .map(entry -> entry.getKey())
          .collect(Collectors.toList());

    for (String key : keys) {
      System.out.println(key);
    }
  }
}

If you are using , here's also a way to do this: 如果您使用的是 ,这里也可以这样做:

List<String> keys = unsortedMap.entrySet()
                   .stream()
                   .sorted((e1, e2) -> Integer.compare(e2.getValue().size(), e1.getValue().size()))
                   .map(Map.Entry::getKey)
                   .collect(Collectors.toList());
System.out.println(keys); //[C, A, B]

What it does is: 它的作用是:

  • get a Stream of the entries of your map 获取地图条目的流
  • sort the entries by the the size of each arraylist 按每个arraylist的大小对条目进行排序
  • map each entry to its corresponding key 将每个条目映射到其对应的密钥
  • collect the result in a List 在List中收集结果

If you want you could also write the sorted line as : 如果您愿意,您也可以将排序行写为:

.sorted(Comparator.comparing(e -> e.getValue().size(), Comparator.reverseOrder()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM