Java学习笔记之HashMap
2011-12-15 22:21
561 查看
HashMap用法 [折叠] package hashmap; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.TreeMap; public class HashMap1 { //初始化 private void init(Map map,String kind) { if(map != null) { for(int i=1; i<6; i++) { map.put(String.valueOf(i),kind+i); } } } //结果输出 private void outPut(Map map) { if(map != null) { Object key = null; Object value = null; Iterator iterater = map.keySet().iterator(); while(iterater.hasNext()) { key = iterater.next(); value = map.get(key); System.out.print(key+": "+value+"/t"); } System.out.println("/n"); } } public static void main(String args[]) { HashMap hashmap = new HashMap(); hashmap.put("x", "1"); hashmap.put("u", "2"); hashmap.put("z", "3"); hashmap.put("h", "4"); hashmap.put("a", "5"); hashmap.put("o", "6"); hashmap.put("g", "7"); hashmap.put("u", "8"); hashmap.put("a", "9"); hashmap.put("n", "10"); hashmap.put("g", "11"); Object key = null; Object value = null; Iterator iterater = hashmap.keySet().iterator(); while(iterater.hasNext()) { key = iterater.next(); value = hashmap.get(key); System.out.print(key+": "+value+"/t"); } System.out.println("/n"); } //声明HashMap对象 private void setHashMap() { HashMap hashMap = new HashMap(); init(hashMap,"HashMap"); hashMap.put(null,"键值为空"); hashMap.put("值为空",null); System.out.println("这是HashMap对象的键与值:"); outPut(hashMap); } //声明Hashtable对象 private void setHashtable(){ Hashtable hashtable = new Hashtable(); init(hashtable,"Hashtable"); //hashtable.put(null,"键值为空"); Hashtable不允许键或值为null; //hashtable.put("值为空",null); System.out.println("这是Hashtable对象的键与值:"); outPut(hashtable); } //声明LinkedHashMap对象 private void setLinkedHashMap(){ LinkedHashMap linkedHashMap = new LinkedHashMap(); init(linkedHashMap,"LinkedHashMap"); linkedHashMap.put(null,"键值为空"); linkedHashMap.put("值为空",null); System.out.println("这是LinkedHashMap对象的键与值:"); outPut(linkedHashMap); } //声明TreeMap对象 private void setTreeMap(){ TreeMap treeMap = new TreeMap(); //TreeMap treeMap = new TreeMap(new MySort());//按自定义的方式排序 init(treeMap,"TreeMap"); treeMap.put("0", "后插入的值"); //treeMap.put(null,"键值为空"); TreeMap不允许键或值为null //treeMap.put("值为空",null); System.out.println("这是TreeMap对象的键与值:"); outPut(treeMap); } // public static void main(String[] args){ // HashMapDemo tm = new HashMapDemo(); // tm.setHashMap(); // tm.setHashtable(); // tm.setLinkedHashMap(); // tm.setTreeMap(); // // Map hashMap = new HashMap(); // hashMap.put(null, "键值为null"); // hashMap.put("值为null", null); // System.out.println("新建HashMap对象元素的记录数是:"+hashMap.size()); // hashMap.remove(null); // System.out.println("删除键值为null的HashMap对象元素的记录数是:"+hashMap.size()); // }
相关文章推荐
- JSP学习笔记:Java中HashMap,LinkedHashMap,TreeMap的区别
- java学习笔记之WeakHashMap 、IdentityHashMap、EnumMap
- Java学习笔记--HashMap
- java 集合学习笔记4-HashMap TreeMap
- 0017 Java学习笔记-集合-集合一般:HashSet和HashMap
- Java学习笔记之HashMap和Hashtable的区别
- 0014 Java学习笔记-集合-HashMap集合
- 自学Java系列 笔记5 Java学习之HashMap和Hashtable的区别
- java学习笔记之HashMap经典分拣存储
- Java 学习笔记 05 HashMap 和 Hashtable 区别与联系
- 集合Map,HashMap、TreeMap+JAVA学习笔记-DAY18
- 【Java学习笔记】Map,TreeMap及HashMap
- Java学习笔记(二二)——Java HashMap
- java学习笔记——HashMap的泛型容器
- 【Java 学习笔记】 Hashmap
- Java集合源码学习笔记(四)HashMap分析
- Java 容器类学习笔记1--了解Java提供的容器类List、ArrayList、Vector及map、HashTable、HashMap
- JAVA学习笔记(二十二)- 集合HashMap与Hashtable
- JAVA学习笔记38——模拟实现Iterator+HashMap的“分拣”原理+“分拣”的应用
- Java学习笔记之集合(八):HashMap的存储原理