HashMap TreeMap Hashtable数据的存储差异。

来源:互联网 发布:阿里服务器绑定域名 编辑:程序博客网 时间:2024/06/05 15:52

直接代码:

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.TreeMap;

 

public class hello {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Map hashtable = new Hashtable();
  hashtable.put("a", "aaa");
  hashtable.put("b", "bbb");
  hashtable.put("c", "ccc");
  hashtable.put("d","ddd");
  java.util.Iterator iterator1 = hashtable.keySet().iterator();
  System.out.println("hashtable:");
  while(iterator1.hasNext())
  {
   Object key = iterator1.next();
   System.out.println(hashtable.get(key));
  }
  
  Map hashmap = new HashMap();
  hashmap.put("a", "aaa");
  hashmap.put("b", "bbb");
  hashmap.put("c", "ccc");
  hashmap.put("d","ddd");
  java.util.Iterator iterator2 = hashmap.keySet().iterator();
  System.out.println("hashmap:");
  while(iterator2.hasNext())
  {
   Object key = iterator2.next();
   System.out.println(hashmap.get(key));
  }
  
  Map treemap = new TreeMap();
  treemap.put("a", "aaa");
  treemap.put("b", "bbb");
  treemap.put("c", "ccc");
  treemap.put("d","ddd");
  java.util.Iterator iterator3 = treemap.keySet().iterator();
  System.out.println("treemap:");
  while(iterator3.hasNext())
  {
   Object key = iterator3.next();
   System.out.println(treemap.get(key));
  }

  

 }

}

运行结果:

 

原创粉丝点击