Last active
February 26, 2020 13:16
-
-
Save benjjo/c679cc54f4980e5a2e367fe9011c89d4 to your computer and use it in GitHub Desktop.
Sorts an mapped array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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