2

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
}
nfroidure
  • 1,531
  • 12
  • 20

1 Answers1

3

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
}
nfroidure
  • 1,531
  • 12
  • 20