Java中的Map总结

(一).参考文档:http://cmsblogs.com/?p=176

(二).Map接口

  1. Map中的元素都是成对出现的,它提供了键(key)到值(vaules)的映射,键决定了元素的存储位置。
  2. 检索Map中的元素需要通过指定key值,key可以是任意数据类型,但key必须唯一,否则新添加的值会取代已有的值。
  3. http://www.java2s.com/Tutorials/Java/Java_Collection/0180__Java_Map.htm

(三)Map接口的实现类HashMap

定义:https://baike.so.com/doc/869853-919684.html

HashMap的实现原理:http://blog.csdn.net/vking_wang/article/details/14166593
http://www.importnew.com/7099.html

代码演示:http://www.cnblogs.com/xwdreamer/archive/2012/05/14/2499339.html

1.Key值相同的结果,被后面一个值覆盖

package com.Collection;

import java.util.HashMap;

public class HashMapDemo {
 
    public static void main(String[] args){
        
        HashMap<Integer,String> studentMap = new HashMap<>();
        
        studentMap.put(1,"李四");
        studentMap.put(1, "凯耐");
        
        System.out.println(studentMap.get(1)); 
    }
    
}
 

2.Key&Values都可以为空值

package com.Collection;

import java.util.HashMap;

public class HashMapDemo {
 
    public static void main(String[] args){
        
        HashMap<Integer,String> studentMap = new HashMap<>();
        
        studentMap.put(1,"李四");
        studentMap.put(null, "凯耐");
        studentMap.put(1, null);
        
        System.out.println(studentMap.get(1)); 
    }
    
}
 

3.删除条目

package com.Collection;

import java.util.HashMap;

public class HashMapDemo {
 
    public static void main(String[] args){
        
        HashMap<Integer,String> studentMap = new HashMap<>();
        
        studentMap.put(1,"李四");
        studentMap.put(2, "凯耐");
        studentMap.put(3, null);
        //通过键值删除条目
        System.out.println(studentMap.remove(3));
        System.out.println("studentMap"+studentMap); 
    }
    
}
 
 清除所有条目使用 Clear()方法

4.在HashMap中检索键、值和键值对

package com.Collection;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
 
public class HashMapDemo {
 
    public static void main(String[] args){
        
        HashMap<Integer,String> studentMap = new HashMap<>();
        
        studentMap.put(1,"张三");
        studentMap.put(2, "李四");
        
        //1.获取Set对象,Set对象保存的数据为整型
        Set<Integer> keys = studentMap.keySet();
        
        //2.获取HashMap的值集合
        Collection<String> values = studentMap.values();
        
        //3.获取Map.Entry object
        Set<Entry<Integer, String>> entries = studentMap.entrySet();
        
        //4.打印所有的Key值
        for(Integer key:keys)
            System.out.println(key); 
        
        //5.打印所有的值
        for(String value:values)
            System.out.println(value);
        
        //打印key-value键值对
        for(Map.Entry entry : entries)
            System.out.println(entry.getKey() + " : " + entry.getValue());
        
    }
    
}
 

控制台打印结果:

1
2
张三
李四
1 : 张三
2 : 李四

(四)java中Map遍历的四种方法
在java中所有的map都实现了Map接口,因此所有的Map(如HashMap, TreeMap, LinkedHashMap, Hashtable等)都可以用以下的方式去遍历。
方法一:在for循环中使用entries实现Map的遍历:

/**
* 最常见也是大多数情况下用的最多的,一般在键值对都需要使用
 */
Map <String,String>map = new HashMap<String,String>();
map.put("熊大", "棕色");
map.put("熊二", "黄色");
for(Map.Entry<String, String> entry : map.entrySet()){
    String mapKey = entry.getKey();
    String mapValue = entry.getValue();
    System.out.println(mapKey+":"+mapValue);
}

方法二:在for循环中遍历key或者values,一般适用于只需要map中的key或者value时使用,在性能上比使用entrySet较好;

Map <String,String>map = new HashMap<String,String>();
map.put("熊大", "棕色");
map.put("熊二", "黄色");
//key
for(String key : map.keySet()){
    System.out.println(key);
}
//value
for(String value : map.values()){
    System.out.println(value);
}

方法三:通过Iterator遍历;

Iterator<Entry<String, String>> entries = map.entrySet().iterator();
while(entries.hasNext()){
    Entry<String, String> entry = entries.next();
    String key = entry.getKey();
    String value = entry.getValue();
    System.out.println(key+":"+value);
}

方法四:通过键找值遍历,这种方式的效率比较低,因为本身从键取值是耗时的操作;



for(String key : map.keySet()){
    String value = map.get(key);
    System.out.println(key+":"+value);
}


参考:https://www.cnblogs.com/damoblog/p/9124937.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值