SortedMap implements Map<K, V> { public SortedMap(Map<K,V> to_sort, final Comparator<? super V> cmp) { Objects.requireNonNull(to_sort, "Map to sort param must not be null"); Objects.requireNonNull(cmp, "comparator param must not be null"); this.index = to_sort; ArrayList<V> iterable_values = new ArrayList<>(to_sort.size()); iterable_values.addAll(to_sort.values()); Collections.sort(iterable_values, cmp); this.iterableValues = Collections.unmodifiableList(iterable_values); ArrayList<K> iterable_keys = new ArrayList<>(to_sort.size()); iterable_keys.addAll(to_sort.keySet()); Collections.sort(iterable_keys, new Comparator<K>() { @Override public int compare(K o1, K o2) { return cmp.compare(index.get(o1), index.get(o2)); } }); this.iterableKeys = Collections.unmodifiableList(iterable_keys); } SortedMap(Map<K,V> to_sort, final Comparator<? super V> cmp); @Override int size(); @Override boolean isEmpty(); @Override boolean containsKey(Object key); @Override boolean containsValue(Object value); @Override V get(Object key); @Override Set<K> keySet(); @Override Collection<V> values(); @Override Set<Entry<K, V>> entrySet(); @Override V put(K key, V value); @Override V remove(Object key); @Override void putAll(Map<? extends K, ? extends V> m); @Override void clear();  }
@Test public void sortedMapTest() { SortedMap<Integer, String> sorted_map = new SortedMap<>(dataSet, cmp); Assert.assertEquals(sorted_map.size(), dataSet.size()); for (Map.Entry<Integer, String> entry: sorted_map.entrySet()) { Assert.assertSame(entry.getValue(), dataSet.get(entry.getKey())); } Assert.assertTrue(sorted_map.values().containsAll(dataSet.values())); Assert.assertTrue(dataSet.values().containsAll(sorted_map.values())); Assert.assertTrue(sorted_map.keySet().containsAll(dataSet.keySet())); Assert.assertTrue(dataSet.keySet().containsAll(sorted_map.keySet())); }