Java 8 Collectors.toMap SortedMap

后端 未结 5 669
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 02:28

I\'m using Java 8 lambdas and want to use Collectors toMap to return a SortedMap. The best I can come up with is to call the following

5条回答
  •  长情又很酷
    2020-12-01 03:14

    Another way you can do this is to allow Collectors.toMap() to return whatever map it is going to return, and then pass that to a new TreeMap<>().

    The caveat there is that this only works if your "hashCode()+equals()" and "compareTo" are consistent. If they aren't consistent, then you'll end up with the HashMap removing different set of keys than your TreeMap.

提交回复
热议问题