Sortable Map

Yesterday I was using a HashMap, and I wanted a quick snippet of code that allowed me to sort the map by either it’s keys or values. I’ve only scratched the surface of all the java Collections classes, so I don’t know if this already exists, but I present a sortable map!

Usage:

SortableMap<String, Integer> map = new SortableMap<String, Integer>();

map.put("Bubble", Integer.valueOf(2));
map.put("Grape", Integer.valueOf(1));

map.sortValues();

for (String key : map.getKeys()) {
	System.out.println(key + " -> " + map.get(key));
}

[icode]SortableMap.java[/icode]

package com.digiturtle.game;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class SortableMap<K extends Comparable<K>, V extends Comparable<V>> {
	
	private ArrayList<K> keys = new ArrayList<K>();
	
	private ArrayList<V> values = new ArrayList<V>();
	
	public void put(K key, V value) {
		keys.add(key);
		values.add(value);
	}
	
	public V get(K key) {
		return values.get(keys.indexOf(key));
	}
	
	public void remove(K key) {
		values.remove(get(key));
		keys.remove(key);
	}
	
	public boolean containsKey(K key) {
		return keys.indexOf(key) >= 0;
	}
	
	public void sortKeys() {
		sortKeys(null);
	}
	
	public void sortKeys(Comparator<K> comparator) {
		ArrayList<K> clonedKeys = new ArrayList<K>();
		ArrayList<V> clonedValues = new ArrayList<V>();
		clonedKeys.addAll(keys);
		clonedValues.addAll(values);
		if (comparator == null)
			Collections.sort(keys);
		else
			Collections.sort(keys, comparator);	// sort the keys
		// add all values
		values.clear();
		for (K key : keys) {
			values.add(clonedValues.get(clonedKeys.indexOf(key)));
		}
	}
	
	public void sortValues() {
		sortValues(null);
	}
	
	public void sortValues(Comparator<V> comparator) {
		ArrayList<K> clonedKeys = new ArrayList<K>();
		ArrayList<V> clonedValues = new ArrayList<V>();
		clonedKeys.addAll(keys);
		clonedValues.addAll(values);
		if (comparator == null)
			Collections.sort(values);
		else
			Collections.sort(values, comparator);	// sort the keys
		// add all values
		keys.clear();
		for (V value : values) {
			keys.add(clonedKeys.get(clonedValues.indexOf(value)));
		}
	}
	
	public ArrayList<K> getKeys() {
		return keys;
	}
	
	public ArrayList<V> getValues() {
		return values;
	}
	
	public static void performUnitTest(String[] keySet, String[] valueSet) {
		SortableMap<String, String> map = new SortableMap<String, String>();
		for (int index = 0; index < keySet.length; index++) {
			map.put(keySet[index], valueSet[index]);
		}
		System.out.println("Start unit test");
//		map.sortKeys();
		map.sortValues();
		for (String key : map.getKeys()) {
			System.out.println(key + " -> " + map.get(key));
		}
		System.out.println("Stop unit test");
	}

}

This is just a snippet that I wrote for my own uses, and thought I should post it just in case anybody else has an interest :slight_smile:

I think you’d be wanting Treemap.

Yeah, a TreeMap will be much faster than this impl and also maintains it’s order without having to remember to sort after insertions.
If any semblance of speed is required, don’t use indexOf (or Collections.sort) in everything…