相同点:
HashMap,LinkedHashMap,TreeMap都属于Map;用于存储键值对(key-value),根据键得到值。键不允许重复,值允许重复。
不同点:
- HashMap根据键的HashCode为索引存储数据。键值对取出时是随机的。
- TreeMap键值对取出是排序的。
- LinkedHashMap是HashMap的子类。键值对输入顺序和取出顺序是相同的。
测试代码:
public class MapTest {
public static void main(String[] args) {
try {
hashMapTest();
treeMapTest();
likedHashMapTest();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void hashMapTest() throws Exception {
System.out.println("--------HashMap--------");
Map<String, String> map = new HashMap<String, String>();
map.put("1", "map test 1");
map.put("2", "map test 2");
map.put("3", "map test 3");
map.put("a", "map test a");
map.put("b", "map test b");
map.put("c", "map test c");
Iterator<Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
System.out.println("Key: " + entry.getKey() + " Value: "
+ entry.getValue());
}
}
public static void treeMapTest() throws Exception {
System.out.println("--------TreeMap--------");
Map<String, String> map = new TreeMap<String, String>();
map.put("1", "map test 1");
map.put("2", "map test 2");
map.put("3", "map test 3");
map.put("a", "map test a");
map.put("b", "map test b");
map.put("c", "map test c");
Iterator<Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
System.out.println("Key: " + entry.getKey() + " Value: "
+ entry.getValue());
}
}
public static void likedHashMapTest() throws Exception {
System.out.println("--------LikedHashMap--------");
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("1", "map test 1");
map.put("2", "map test 2");
map.put("3", "map test 3");
map.put("a", "map test a");
map.put("b", "map test b");
map.put("c", "map test c");
Iterator<Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
System.out.println("Key: " + entry.getKey() + " Value: "
+ entry.getValue());
}
}
}
结果:
--------HashMap--------
Key: 1 Value: map test 1
Key: a Value: map test a
Key: 2 Value: map test 2
Key: b Value: map test b
Key: 3 Value: map test 3
Key: c Value: map test c
--------TreeMap--------
Key: 1 Value: map test 1
Key: 2 Value: map test 2
Key: 3 Value: map test 3
Key: a Value: map test a
Key: b Value: map test b
Key: c Value: map test c
--------LikedHashMap--------
Key: 1 Value: map test 1
Key: 2 Value: map test 2
Key: 3 Value: map test 3
Key: a Value: map test a
Key: b Value: map test b
Key: c Value: map test c
Key: 1 Value: map test 1
Key: a Value: map test a
Key: 2 Value: map test 2
Key: b Value: map test b
Key: 3 Value: map test 3
Key: c Value: map test c
--------TreeMap--------
Key: 1 Value: map test 1
Key: 2 Value: map test 2
Key: 3 Value: map test 3
Key: a Value: map test a
Key: b Value: map test b
Key: c Value: map test c
--------LikedHashMap--------
Key: 1 Value: map test 1
Key: 2 Value: map test 2
Key: 3 Value: map test 3
Key: a Value: map test a
Key: b Value: map test b
Key: c Value: map test c