How to use SortedMap interface or TreeMap in Java?

I would use TreeMap, which implements SortedMap. It is designed exactly for that. Example: Map<Integer, String> map = new TreeMap<Integer, String>(); // Add Items to the TreeMap map.put(1, “One”); map.put(2, “Two”); map.put(3, “Three”); // Iterate over them for (Map.Entry<Integer, String> entry : map.entrySet()) { System.out.println(entry.getKey() + ” => ” + entry.getValue()); } See the Java … Read more

Create a SortedMap in Java with a custom Comparator

You can use a custom comparator like this: Comparator<String> secondCharComparator = new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.substring(1, 2).compareTo(s2.substring(1, 2)); } }; Sample: SortedMap<String,String> map = new TreeMap<String,String>(secondCharComparator); map.put(“Za”, “FOO”); map.put(“Ab”, “BAR”); map.put(“00”, “ZERO”); System.out.println(map); // prints “{00=ZERO, Za=FOO, Ab=BAR}” Note that this simply assumes that the String has … Read more

How to sort a treemap based on its values?

Here is a solution: public static <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) { Comparator<K> valueComparator = new Comparator<K>() { public int compare(K k1, K k2) { int compare = map.get(k2).compareTo(map.get(k1)); if (compare == 0) return 1; else return compare; } }; Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator); sortedByValues.putAll(map); return sortedByValues; … Read more

Using java.util.Map in h:dataTable

Until upcoming JSF 2.3, UIData components such as <h:dataTable>, <p:dataTable>, etc and <ui:repeat> does not support iterating over a Map. This is only supported in <c:forEach>. One way is to convert the map entries to an array (alone entrySet() won’t work as UIData also doesn’t support Set until upcoming JSF 2.3). <h:dataTable value=”#{bean.map.entrySet().toArray()}” var=”entry”> <h:column>#{entry.key}</h:column> … Read more

Java List Sorting: Is there a way to keep a list permantly sorted automatically like TreeMap?

You can change the behaviour of ArrayList List<MyType> list = new ArrayList<MyType>() { public boolean add(MyType mt) { super.add(mt); Collections.sort(list, comparator); return true; } }; Note: a PriorityQueue is NOT a List, if you didn’t care what type of collection it was, the simplest would be to use a TreeSet, which is just like a … Read more

Java TreeMap Comparator

You can not sort TreeMap on values. A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used You will need to provide comparator for Comparator<? super K> so your comparator should compare … Read more

Struts2: Updating the values of a “List Of Objects” inside a Map

According to your latest update. If you are using TreeMap Struts2 cannot correctly determine type of elements inside it. Change declaration of testTreeMap from TreeMap to Map. private Map<String,ObjectCList> testTreeMap = new TreeMap<String,ObjectCList>(); Or annotate testTreeMap with com.opensymphony.xwork2.util.Element annotation to tell Struts2 what type are elements inside map. @Element(value = ObjectCList.class) private TreeMap<String,ObjectCList> testTreeMap = … Read more