Skip to content

Instantly share code, notes, and snippets.

@benjjo
Last active February 26, 2020 13:16
Show Gist options
  • Save benjjo/c679cc54f4980e5a2e367fe9011c89d4 to your computer and use it in GitHub Desktop.
Save benjjo/c679cc54f4980e5a2e367fe9011c89d4 to your computer and use it in GitHub Desktop.
Sorts an mapped array
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
public class Sorter {
SortedMap<Integer, String> sm = new TreeMap<>();
Set s = sm.entrySet();
/**
* Constructor
*/
public Sorter() {
sm.put(2, "java");
sm.put(3, "python");
sm.put(1, "pearl");
sm.put(4, "c");
this.doit();
}
/**
* Does it all
*/
public void doit() {
Iterator i = this.s.iterator();
if (i.hasNext()) {
do {
Map.Entry m = (Map.Entry) i.next();
int key = (Integer) m.getKey();
String value = (String) m.getValue();
System.out.println("Key: " + key + " value: " + value);
} while (i.hasNext());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment