简体   繁体   English

HashMap到TreeMap

[英]HashMap to TreeMap

I am using TreeMap to sort the keys in the Map . 我正在使用TreeMapMap的键进行排序。

Map<Byte, List<TagEntity>> hashMap = list.stream().collect(Collectors.groupingBy(TagEntity::getTagType));

Map<Byte, List<TagEntity>> treeMap = new TreeMap<>(Comparator.reverseOrder());

But how to convert HashMap to TreeMap ? 但是如何将HashMap转换为TreeMap

You can create the TreeMap directly by passing a map supplier to groupingBy : 您可以通过将地图供应商传递给groupingBy来直接创建TreeMap

Map<Byte, List<TagEntity>> treeMap = 
    list.stream()
        .collect(Collectors.groupingBy(TagEntity::getTagType,
                                      () -> new TreeMap<Byte, List<TagEntity>>(Comparator.reverseOrder()),
                                      Collectors.toList()));

This is how you can create hashmap to treemap: 这是你可以创建hashmap到treemap的方法:

HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
treeMap.putAll(hashMap);

Refer here java-putting-hashmap-into-treemap 请参阅java-puts-hashmap-into-treemap

This should work: 这应该工作:

TreeMap treeMap = new TreeMap<>(hashMap); TreeMap treeMap = new TreeMap <>(hashMap);

Or: 要么:

treeMap.putAll(hashMap); treeMap.putAll(HashMap中);

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

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