在Java中按值的顺序返回hashmap键?

在Java中按值的顺序返回hashmap键?,java,stream,hashmap,Java,Stream,Hashmap,有办法做到这一点吗?我想这可以通过流来完成,但我对它们不是很有经验,我想得到一些帮助 例如:如果hashmap当前类似: “A”:5 “B”:2 “C”:4 然后返回[“A”、“C”、“B”]。Map-sortedMap=unsortMap.entrySet().stream() .sorted(Map.Entry.comparingByKey()) .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue, (oldValu

有办法做到这一点吗?我想这可以通过流来完成,但我对它们不是很有经验,我想得到一些帮助

例如:如果hashmap当前类似:

“A”:5

“B”:2

“C”:4

然后返回[“A”、“C”、“B”]。

Map-sortedMap=unsortMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,
(oldValue,newValue)->oldValue,LinkedHashMap::new));
Map sortedMap=unsortMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,
(oldValue,newValue)->oldValue,LinkedHashMap::new));

您可以使用TreeMap,它将按键为您排序

List<Character> sortedKey() {
    Map<Character, Integer> map = new TreeMap<>();
    List<Character> list = new ArrayList<>();
    for(Map.Entry entry: map.entrySet()) {
        list.add((Character)entry.getKey());
    }
    return list;
}
List sortedKey(){
Map Map=newtreemap();
列表=新的ArrayList();
对于(Map.Entry:Map.entrySet()){
添加((字符)entry.getKey());
}
退货清单;
}

您可以使用TreeMap,它将按键为您排序

List<Character> sortedKey() {
    Map<Character, Integer> map = new TreeMap<>();
    List<Character> list = new ArrayList<>();
    for(Map.Entry entry: map.entrySet()) {
        list.add((Character)entry.getKey());
    }
    return list;
}
List sortedKey(){
Map Map=newtreemap();
列表=新的ArrayList();
对于(Map.Entry:Map.entrySet()){
添加((字符)entry.getKey());
}
退货清单;
}

使用
集合。排序方法:

  • 将HashMap转换为列表
  • 使用
    自定义比较器对列表进行排序
  • 然后创建
    键的列表(根据其值排序)
  • 代码:

    //按值对hashmap排序的函数
    公共静态列表sortByValue(HashMaphm){
    //从HashMap的元素创建列表
    列表>列表=
    新建LinkedList>(hm.entrySet());
    //对列表排序
    Collections.sort(列表,新比较器>(){
    公共整数比较(Map.Entryo1,
    Map.Entryo2){
    return(o2.getValue()).compareTo(o1.getValue());
    }
    });
    //将排序列表中的数据放入hashmap
    Listtemp=newarraylist();
    对于(Map.Entryaa:list){
    临时添加(aa.getKey());
    }
    返回温度;
    }
    
    使用
    集合。排序方法:

  • 将HashMap转换为列表
  • 使用
    自定义比较器对列表进行排序
  • 然后创建
    键的列表(根据其值排序)
  • 代码:

    //按值对hashmap排序的函数
    公共静态列表sortByValue(HashMaphm){
    //从HashMap的元素创建列表
    列表>列表=
    新建LinkedList>(hm.entrySet());
    //对列表排序
    Collections.sort(列表,新比较器>(){
    公共整数比较(Map.Entryo1,
    Map.Entryo2){
    return(o2.getValue()).compareTo(o1.getValue());
    }
    });
    //将排序列表中的数据放入hashmap
    Listtemp=newarraylist();
    对于(Map.Entryaa:list){
    临时添加(aa.getKey());
    }
    返回温度;
    }
    
    给定

    Map<String,Integer> map = Map.of("A",5,"B",2,"C",4);
    
    印刷品

    [A, C, B]
    
    给定

    印刷品

    [A, C, B]
    

    使用方法witch创建Map.Entries和sort List的新列表,该方法返回一个比较器,该比较器将Map.Entries按自然顺序与值进行比较,然后按相反顺序进行转换:

    List<Map.Entry<String,Integer>> list = new ArrayList<>(map.entrySet());
    list.sort(Map.Entry.<String, Integer>comparingByValue().reversed());
    System.out.println(list);
    
    List List=newarraylist(map.entrySet());
    list.sort(Map.Entry.comparingByValue().reversed());
    系统输出打印项次(列表);
    
    使用方法witch创建映射条目和排序列表的新列表,该方法返回一个比较器,将映射条目按自然顺序与值进行比较,然后按相反顺序进行转换:

    List<Map.Entry<String,Integer>> list = new ArrayList<>(map.entrySet());
    list.sort(Map.Entry.<String, Integer>comparingByValue().reversed());
    System.out.println(list);
    
    List List=newarraylist(map.entrySet());
    list.sort(Map.Entry.comparingByValue().reversed());
    系统输出打印项次(列表);
    
    我想你的意思是
    通过值进行比较
    。此外,OP只是请求密钥,您不需要返回地图。是的,更改后的流会是什么样子?另外,为了加强我对流的理解,您是否可以详细分析一下这段代码的作用?我知道您获取
    入口集
    ,将其转换为流,根据
    comparingByKey
    提供的
    比较器
    进行排序,然后将其收集回来,但我不太了解
    collect
    方法,或者更具体地说,
    toMap
    方法中的内容,考虑到这个问题,我认为OP显然希望以排序的方式检索所有键。不是整个地图,我想你指的是
    comparingByValue
    。此外,OP只是请求密钥,您不需要返回地图。是的,更改后的流会是什么样子?另外,为了加强我对流的理解,您是否可以详细分析一下这段代码的作用?我知道您获取
    入口集
    ,将其转换为流,根据
    comparingByKey
    提供的
    比较器
    进行排序,然后将其收集回来,但我不太了解
    collect
    方法,或者更具体地说,
    toMap
    方法中的内容,考虑到这个问题,我认为OP显然希望以排序的方式检索所有键。不是整张地图都整理好了