HashMap<ArrayList<Integer>, String>
Iterating though this structure, I get:
[1 1 0], "gh"
[0 4 2], "gh"
[0 5 2], "qh"
However, I want to sort this on the key (in increasing order), to get
[0 4 2], "gh"
[0 5 2], "qh"
[1 1 0], "gh"
The first component is sorted first, then the second, then the third.
HashMaps have no inherent ordering.Use a different data structure, or externally sort the keys yourself, before iterating. Either way, you’ll need to write a
Comparator<List<Integer>>sinceList<E>does not extendComparable<List<E>>N.B. it is generally a Bad Idea to use mutable keys in a map of any type. Personally, I’ve never had a need to – at least use an immutable list of integers instead.