map常用方法

Map常用方法:


package com.cheng.collection;
?
import org.junit.Test;
?
import java.util.*;
?
?
public class MapMethord {
   //添加、修改、删除操作:
   @Test
   public void methordTest(){
       Map map = new HashMap();
       //put 添加
       map.put("a",1);
       map.put("b",2);
       map.put("c",3);
       //put 当key相等时,在put就是修改value
       map.put("a",56);
?
       System.out.println(map);//{a=56, b=2, c=3}
?
       Map map1 = new HashMap();
       map1.put(123,"aa");
       map1.put(456,"bb");
?
       map.putAll(map1);//
       System.out.println(map);//{a=56, b=2, c=3, 456=bb, 123=aa}
?
       Object value = map.remove("a");//value = 56
       Object value2 = map.remove("aa");//value2 = null 若是没有key为aa的 返回null
       System.out.println(value);//56
       System.out.println(value2);//null
       System.out.println(map);//{b=2, c=3, 456=bb, 123=aa}
?
       //清空map
       map.clear();
       System.out.println(map.size());//0
       System.out.println(map);//{}
?
       //get 获取指定key对应的value
       //Object o = map1.get(123);
       //System.out.println(o); 也是输出aa
       System.out.println(map1.get(123));//aa
?
       //是否包含指定的key和value 找到就不会往下找
       System.out.println(map1.containsKey(123));//true
       System.out.println(map1.containsValue("bb"));//true
?
       //size 获取当前集合内key-value对的个数
       System.out.println(map1.size());//2
?
       //isEmpty()判断是否为空
       System.out.println(map.isEmpty());//true
       System.out.println(map1.isEmpty());//false
?
       //map.equals(obj) 判断obj和 map 是否相同 结果是boolean类型的
       Map map2 = new HashMap();
       map2.put(123,"aa");
       map2.put(456,"bb");
       System.out.println(
上一篇:log4j的配置


下一篇:Nginx 服务优化与防盗链