首页 > 其他 > 详细

map常用方法

时间:2021-08-16 10:26:53      阅读:15      评论:0      收藏:0      [点我收藏+]

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(map1.equals(map2));//true
?
  }
   @Test
   public void test02(){
?
       //   map遍历的操作方法:
       //   Set keySet()         返回所有key构成的Set集合
       //   Collection values()   返回所有value构成的Collection集合
       //   Set entrySet()       返回所有key-value对构成的Set集合
       Map map = new HashMap();
       map.put(1,"a");
       map.put(2,"b");
       map.put(3,"c");
       map.put(4,"d");
       map.put(5,"e");
?
       //遍历所有的key集
       Set set = map.keySet();//获取map的key集
       Iterator iterator = set.iterator();//迭代器遍历
       while (iterator.hasNext()){
           System.out.print(iterator.next()+"\t");
      }//1 2 3 4 5
       System.out.println();
?
       //遍历所有的value集
       Collection values = map.values();//获取map的value集
       Iterator iterator1 = values.iterator();//迭代器遍历
       while (iterator1.hasNext()){
           System.out.print(iterator1.next()+"\t");
      }//a b c d e
       System.out.println();
?
       //遍历所有的key-value
       //方式1
       Set set1 = map.entrySet();
       Iterator iterator2 = set1.iterator();
       while (iterator2.hasNext()){
           //entrySet集合中的元素都是set
           Object obj = iterator2.next();
           Map.Entry entry = (Map.Entry) obj;
           System.out.println(entry.getKey() + "---->" + entry.getValue());
           //1---->a
           //2---->b
           //3---->c
           //4---->d
           //5---->e
           //getKey和getValue 分别是获取entry中的key和value
      }
?
       //方式2
       Set keySet = map.keySet();//获取map的key集
       Iterator iterator3 = keySet.iterator();
       while (iterator3.hasNext()){
           Object next = iterator3.next();
           //next存 key
           Object o = map.get(next);
           //o存 next对应的value
           System.out.println(next + "====" + o);
//           1====a
//           2====b
//           3====c
//           4====d
//           5====e
      }
       System.out.println();
?
?
  }
}
?

 

map常用方法

原文:https://www.cnblogs.com/Alirious/p/15146198.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!