How can I swap an item's order in a TreeMap
in Kotlin? Something like Collections.swap()
but on a map, not on a list. Like swapping an item's position with another item.
A TreeMap is a self-sorting collection, so you can't swap elements. The map elements gets sorted in a tree according to their natural ordering (if they implement Comparable
interface) or according to a custom Comparator
that you need to provide to the map.
More info in the JavaDoc: https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
If arbitrary ordering (and/or re-ordering) is important to you, you may want to consider other kinds of collections, like List
for example, or a combination of multiple collections.