Java学习笔记之HashMap

本文详细介绍了Java中HashMap的使用方法,包括初始化、添加元素、遍历等操作,并对比了HashMap与其他几种Map实现的区别,如Hashtable、LinkedHashMap和TreeMap。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()); // }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值