LinkedHashMap if you want to retrieve the values in the order they are inserted into the Map TreeMap if you want to retrieve in the order of Key HashMap if you are not particular about it
I remembered.
MapTest
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.TreeMap;
import java.util.HashMap;
public class Main {
public static void main(String[] args) throws Exception {
new MapTester(new LinkedHashMap<String, String>()).test();
new MapTester(new HashMap<String, String>()).test();
new MapTester(new TreeMap<String, String>()).test();
}
}
class MapTester {
private Map<String, String> map;
public MapTester(Map<String, String> map) {
this.map = map;
}
public void test() {
this.putValues();
this.dump();
}
private void putValues() {
this.map.put("4", "Geryos");
this.map.put("1", "Airou");
this.map.put("5", "Rio Leia");
this.map.put("2", "Yan Cook");
this.map.put("3", "Dos Jaggi");
this.map.put("6", "Rio Leus");
}
private void dump() {
System.out.println(this.map.getClass().getName() + "Contents");
for (Map.Entry<String, String> entry : this.map.entrySet()) {
System.out.println(entry.getKey() + ":[" + entry.getValue() + "]");
}
System.out.println("");
}
}
Execution result
java.util.Contents of LinkedHashMap
4:[Geryos]
1:[Airou]
5:[Rio Leia]
2:[Yan Cook]
3:[Dos Jaggi]
6:[Rio Leus]
java.util.Contents of HashMap
1:[Airou]
2:[Yan Cook]
3:[Dos Jaggi]
4:[Geryos]
5:[Rio Leia]
6:[Rio Leus]
java.util.Contents of TreeMap
1:[Airou]
2:[Yan Cook]
3:[Dos Jaggi]
4:[Geryos]
5:[Rio Leia]
6:[Rio Leus]
Recommended Posts