Search code examples
elasticsearch-painless

Traversing a HashMap by its sorted keys


I try to figure out how I could iterate on a Painless HashMap values by its keys sorted in ascending order, the following doesn't work:

HashMap buckets;

for(String bucketKey : new TreeSet(buckets.keySet())) {
  // actual code
}

Solution

  • Finally found a way to do this by using ArrayList but absolutely not sure it is the way to go:

    HashMap buckets;
    ArrayList l = new ArrayList(buckets.keySet());
    Collections.sort(l);
    for(String bucketKey : l) {
        // actual code
    }