使用Java8流对HashMap字符串值进行排序';行不通

使用Java8流对HashMap字符串值进行排序';行不通,java,sorting,hashmap,java-8,java-stream,Java,Sorting,Hashmap,Java 8,Java Stream,我使用问题的解决方案对LinkedHashMap中的字符串值进行排序。然而,排序根本不起作用。这是我写的代码 Map<Integer, String> sortedMap = myMap.entrySet().stream() .sorted(Map.Entry.comparingByValue()) .collect(Collectors.toMap(Map.Entry<Integer, String>::

我使用问题的解决方案对
LinkedHashMap
中的字符串值进行排序。然而,排序根本不起作用。这是我写的代码

Map<Integer, String> sortedMap = myMap.entrySet().stream()
                .sorted(Map.Entry.comparingByValue())
                .collect(Collectors.toMap(Map.Entry<Integer, String>::getKey, 
                    Map.Entry<Integer, String>::getValue));

myMap = new LinkedHashMap<Integer, String>(sortedMap);
Map sortedMap=myMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(Map.Entry::getKey、,
Map.Entry::getValue));
myMap=新的LinkedHashMap(sortedMap);

奇怪的是,当同时使用
comparingByValue
comparingByKey
方法时,它对
Integer
键进行排序。因此它肯定是排序,只是不是
字符串
值,而是
整数
键。我不明白我做错了什么。

我猜Collectors.toMap正在一张无序的地图中收集它们,立即破坏了订单

尝试直接在
LinkedHashMap
中收集它们:

LinkedHashMap<Integer, String> newMap = new LinkedHashMap<>();
Map<Integer, String> sortedMap = myMap.entrySet().stream()
                .sorted(Map.Entry.comparingByValue())
                .collect((k, v) -> newMap.put(k, v));
myMap = newMap;
LinkedHashMap newMap=newlinkedhashmap();
Map sortedMap=myMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.collect((k,v)->newMap.put(k,v));
myMap=newMap;


至于整数键被排序的原因:这可能仅仅是巧合,这取决于
HashMap
是如何扣键的。

我猜Collectors.toMap正在无序映射中收集它们,立即破坏了排序

尝试直接在
LinkedHashMap
中收集它们:

LinkedHashMap<Integer, String> newMap = new LinkedHashMap<>();
Map<Integer, String> sortedMap = myMap.entrySet().stream()
                .sorted(Map.Entry.comparingByValue())
                .collect((k, v) -> newMap.put(k, v));
myMap = newMap;
LinkedHashMap newMap=newlinkedhashmap();
Map sortedMap=myMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.collect((k,v)->newMap.put(k,v));
myMap=newMap;


至于整数键被排序的原因:这可能只是巧合,这取决于
HashMap
如何扣紧键。

您正在使用的
toMap
收集器将元素放在
HashMap
中,因此排序在这里没有帮助,因为您最终将它们放在一个非有序的集合中

使用重载的
toMap
方法,并提供一个
LinkedHashMap
作为具体实例,即:

Map<Integer, String> sortedMap = 
     myMap.entrySet()
          .stream()
          .sorted(Map.Entry.comparingByValue())
          .collect(Collectors.toMap(Map.Entry::getKey,
                                    Map.Entry::getValue, 
                                    (a, b) -> a, //or throw an exception
                                    LinkedHashMap::new));
Map-sortedMap=
myMap.entrySet()文件
.stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(Map.Entry::getKey、,
Map.Entry::getValue,
(a,b)->a,//或引发异常
LinkedHashMap::new));

您正在使用的
toMap
收集器将元素放入
HashMap
中,因此排序在这里没有帮助,因为您最终将它们放入了一个非有序的集合中

使用重载的
toMap
方法,并提供一个
LinkedHashMap
作为具体实例,即:

Map<Integer, String> sortedMap = 
     myMap.entrySet()
          .stream()
          .sorted(Map.Entry.comparingByValue())
          .collect(Collectors.toMap(Map.Entry::getKey,
                                    Map.Entry::getValue, 
                                    (a, b) -> a, //or throw an exception
                                    LinkedHashMap::new));
Map-sortedMap=
myMap.entrySet()文件
.stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(Map.Entry::getKey、,
Map.Entry::getValue,
(a,b)->a,//或引发异常
LinkedHashMap::new));

我的猜测是
收集器。toMap
在散列图中收集它们,破坏排序。这是有道理的。但是,这仍然不能解释整型键的排序。整型看起来是排序的,因为整型值本身被用作散列,但是,一旦添加更多整数,您可能会得到不同的顺序,因为重新灰化/多个项目会在同一个存储桶中结束。我猜
收集器。toMap
正在哈希映射中收集它们,破坏顺序。这很有意义。但是,这仍然不能解释整型键的排序。整型键似乎已排序,因为整型值本身被用作散列,但是,一旦添加更多的整型,您可能会得到不同的顺序,因为在同一个存储桶中重新排序/多个项结束。从技术上讲,未指定将返回何种类型的映射
toMap
。当前的Oracle实现确实使用了
HashMap
。是的,很抱歉。我应该说“该实现不保证映射的属性,目前在幕后使用HashMap。如果需要特定的实现,请使用重载的
toMap
方法。”@AlexisC。很抱歉,您在技术上是怎么想的,没有指定将返回什么类型的映射
toMap
。当前的Oracle实现确实使用了
HashMap
。是的,很抱歉。我应该说“该实现不保证映射的属性,目前在幕后使用HashMap。如果需要特定的实现,请使用重载的
toMap
方法。”@AlexisC。对不起,你在想什么