Java: Map sorted by value
November 20th, 2008 by Stefan Fußenegger | Published in JavaToday, I wanted to solve a – at first sight – rather simple problem in Java. I wanted to iterate over the keys and values of a Map in an order given by its values. E.g.
Map m = new ... m.put("foo", 47); m.put("bar", 11); m.put("baz", 11); m.put("qux", 100); // {bar=11, baz=11, foo=47, qux=100} System.out.println(m); m.put("foo", 101); // {bar=11, baz=11, qux=100, foo=101} System.out.println(m);