Java中的HashMap, Hashtable,TreeMap, LinkedHashMap
java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。本站提供基于Java框架struts,spring,hibernate等的桌面应用、web交互及移动终端的开发技巧与资料
保持永久学习的心态,将成就一个优秀的你,来 继续搞起java知识。
stackoverflow
实例说明:
HashMap
1public class Test {
public static void main(String[] args){
Map<String,String> hashmap = new HashMap<String,String>();
hashmap.put("name","range");
hashmap.put("age","25");
hashmap.put("sex","female");
hashmap.put("nationality","PR China");
hashmap.put("education","MD");
hashmap.put(null,null);
hashmap.put(null,"test");
hashmap.put("test",null);
Set<Map.Entry<String,String>> entryset = hashmap.entrySet();
for(Map.Entry<String,String> each : entryset){
System.out.println(each.getKey()+":"+each.getValue());
}
}
}
输出结果:
1null:test education:MD nationality:PR China test:null sex:female name:range age:25
TreeMap
1public class Test {
public static void main(String[] args){
Map<String,String> hashmap = new TreeMap<String,String>();
hashmap.put("name","range");
hashmap.put("age","25");
hashmap.put("sex","female");
hashmap.put("nationality","PR China");
hashmap.put("education","MD");
// hashmap.put(null,null);
// hashmap.put(null,"test");
hashmap.put("test",null);
Set<Map.Entry<String,String>> entryset = hashmap.entrySet();
for(Map.Entry<String,String> each : entryset){
System.out.println(each.getKey()+":"+each.getValue());
}
}
}
输出结果:
1age:25 education:MD name:range nationality:PR China sex:female test:null
LinkedHashMap
1public class Test {
public static void main(String[] args){
Map<String,String> hashmap = new LinkedHashMap<String,String>();
hashmap.put("name","range");
hashmap.put("age","25");
hashmap.put("sex","female");
hashmap.put("nationality","PR China");
hashmap.put("education","MD");
hashmap.put(null,null);
hashmap.put(null,"test");
hashmap.put("test",null);
Set<Map.Entry<String,String>> entryset = hashmap.entrySet();
for(Map.Entry<String,String> each : entryset){
System.out.println(each.getKey()+":"+each.getValue());
}
}
}
输出结果:
1name:range age:25 sex:female nationality:PR China education:MD null:test test:null
hashmaphashtablestackoverflowjava
因为水平有限,难免有疏忽或者不准确的地方,希望大家能够直接指出来,我会及时改正。一切为了知识的分享。
后续会有更多的精彩的内容分享给大家。
支付宝扫一扫
微信扫一扫
