Map类的常用方法

Map类的常用方法

Map类属于集合中的一大类,是双值存储的,同时存储键位和值;其值可以重复但键位是不能重复的,然后它常用的子接口还有HashMap 及TreeMap。本文先主要讲关于Map类的使用方法,之后再整理Collection类的使用。

涉及方法有:1.size(); 2.value(); 3.KeySet(); 4.hashCode();5.get(); 6.replace(); 7.containsValue()和 containsKey() 8.remove(); 9.clear(); 10.isEmpty(); 11.map.putAll(); 12.entrySet().以及三种遍历方式

1.size();

功能:获取键值对数量

代码:

  int l =map.size();
  System.out.println("键值对数为:"+l);

2.value();

功能:得到所有的value,返回值类型为Collection

代码:

   Collection v = map.values();
   System.out.println("所有的值为:"+v);

3.KeySet();

功能:获取所有的键,返回值类型为Set

代码:

    Set s = map.keySet();
	System.out.println("所有的键为:"+s);

4.hashCode();

功能:得到哈希码的总和

代码:

    int c = map.hashCode();
	System.out.println("哈希码总和为:"+c);

5.get(“键值”);

功能:通过key获得value

代码:

    Object o =map.get(1);
    System.out.println("得到的value为:"+o);

6.replace();

功能:通过key替换value

代码:

   //当查找不到键值时,这种替换方法不会出异常,但会提示无法替换
	boolean flag = map.replace(5, "苹果", "大苹果");
   //replace(key,oldValue,newValue) 进行替换操作并返回能否替换
	System.out.println("是否能够替换:"+flag);
	System.out.println(map.get(1));
	//replace(key,newValue)   进行替换
	map.replace(1, "红苹果");       
	System.out.println(map.get(1));

7.containsValue()和 containsKey()

功能:判断是否含有该键位或值

代码:

   boolean f1 = map.containsValue("水晶");
   System.out.println("map中是否有水晶这个内容:"+f1);
   boolean f2 = map.containsKey(3);
   System.out.println("map中是否含有3这个键值:"+f2);

8.remove(“键值”);

功能:移除指定key的数值

代码:

   map.remove("1");
   System.out.println("移除后大小为 "+map.size());

9.clear();

功能:清空map的数值

代码:

   map.clear();
   System.out.println("清空后的map大小为 "+map.size());

10.isEmpty();

功能:判空

代码:

   System.out.println("map是否为空:"+map.isEmpty());

11.map.putAll();

功能:将集合合并

代码:

  Map map3 = new HashMap();
  //添加键值
  map3.put("1", "苹果");
  map3.put("2", "香蕉");
  map3.put("3", "菠萝");
  System.out.println("map3的大小为:"+map3.size());
  //再生成一个实例
  Map map4 = new HashMap();
  map4.put("4", "梨子");
  map4.put("5", "柿子");
  System.out.println("map4的大小为:"+map4.size());	   //把map4合并到map3
  //合并时如果map4的key与map3的key有重复的,则map4的键值覆盖与map3重复的键值
  map3.putAll(map4);
  System.out.println("合并后map3的大小为:"+map3.size());

12.entrySet();

功能:返回一个保存键值对的set集合,便于遍历的取出数据

代码:

        //从Map内部接口Entry
        Entry entry;
		//重新创建实例
		Map map2 = new HashMap();
		//添加键值
		map2.put(1, "苹果");
		map2.put(2, "香蕉");
		map2.put(3, "菠萝");
		//获得键值对的集合
		//entrySet()
		Set s2 = map2.entrySet();
		//调用集合的迭代器
		Iterator iterator = s2.iterator();
		//遍历迭代器
		while(iterator.hasNext()) {
			//遍历出的键值放进entry集合里
			entry=(Map.Entry) iterator.next();
			//得到entry的key  并使用强转类型拆箱
			int key = (int) entry.getKey();
			//得到entry的value  同样地拆箱
			String value = (String) entry.getValue();
			//输出key value
			System.out.println("得到的Key为:"+key);
			System.out.println("得到的value为:"+value);
		}

代码总览

public class Demo1 {
	//Map类
	public static void main(String[] args) {
		//创建map实例
		Map map = new HashMap();
		//添加键值
		map.put(1, "苹果");
		map.put(2, "香蕉");
		map.put(3, "菠萝");
		

		//方法
		//size();
		//获取键值对数量
		int l =map.size();
		System.out.println("键值对数为:"+l);
	
		//value();
		//得到所有的value,返回值类型为Collection
		Collection v = map.values();
		System.out.println("所有的值为:"+v);
		
		//KeySet();
		//获取所有的键,返回值类型为Set
		Set s = map.keySet();
		System.out.println("所有的键为:"+s);
		
		//hashCode()
		//得到哈希码的总和
		int c = map.hashCode();
		System.out.println("哈希码总和为:"+c);		

		//get("键值");
		//通过key获得value
		Object o =map.get(1);
		System.out.println("得到的value为:"+o);
		
		//replace();
		//当查找不到键值时,这种替换方法不会出异常,但会提示无法替换
		boolean flag = map.replace(5, "苹果", "大苹果");
		//replace(key,oldValue,newValue) 进行替换操作并返回能否替换
		System.out.println("是否能够替换:"+flag);
		System.out.println(map.get(1));
		//replace(key,newValue)   进行替换
		map.replace(1, "红苹果");       
		System.out.println(map.get(1));
		
		//判断是否含有该内容或键值
		//containsValue(Object value)  containsKey(Object key)
		boolean f1 = map.containsValue("水晶");
		System.out.println("map中是否有水晶这个内容:"+f1);
		boolean f2 = map.containsKey(3);
		System.out.println("map中是否含有3这个键值:"+f2);
	
		//remove("键值");
		//移除key值为1的数值
		map.remove("1");
		System.out.println("移除后大小为"+map.size());
		
		//clear();
		//清空map的数值		
		map.clear();
		System.out.println("清空后的map大小为:"+map.size());
		
		//isEmpty();
		//判空
		System.out.println("map是否为空:"+map.isEmpty());
	
		//遍历		
		//Entry是Map集合中的一个内部接口,用于封装Map集合中的一组键值(key和value)
		//从Map内部接口Entry
		Entry entry;
		//重新创建实例
		Map map2 = new HashMap();
		//添加键值
		map2.put(1, "苹果");
		map2.put(2, "香蕉");
		map2.put(3, "菠萝");
		//获得键值对的集合
		//entrySet()
		Set s2 = map2.entrySet();
		//调用集合的迭代器
		Iterator iterator = s2.iterator();
		//遍历迭代器
		while(iterator.hasNext()) {
			//遍历出的键值放进entry集合里
			entry=(Map.Entry) iterator.next();
			//得到entry的key  并使用强转类型拆箱
			int key = (int) entry.getKey();
			//得到entry的value  同样地拆箱
			String value = (String) entry.getValue();
			//输出key value
			System.out.println("得到的Key为:"+key);
			System.out.println("得到的value为:"+value);
			
		}
		
		//将两个集合合并
		//map.putAll()
		Map map3 = new HashMap();
		//添加键值
		map3.put("1", "苹果");
		map3.put("2", "香蕉");
		map3.put("3", "菠萝");
		System.out.println("map3的大小为:"+map3.size());
		//再生成一个实例
		Map map4 = new HashMap();
		map4.put("4", "梨子");
		map4.put("5", "柿子");
		System.out.println("map4的大小为:"+map4.size());			
		//把map4合并到map3
		//合并时如果map4的key与map3的key有重复的,则map4的键值覆盖与map3重复的键值
		map3.putAll(map4);
		System.out.println("合并后map3的大小为:"+map3.size());
        
        //再提供两种遍历方式
        //第二种 :使用KeySet,将Map转为Set集合(keySet()),然后通过调用Set的迭代器获取键值
        Set s = map2.keySet();
		Iterator it = s.iterator();
		while(it.hasNext()) {
			int key =(int)it.next();
			String value = (String) map.get(key);
			System.out.println("键位 "+key+"  值   "+value);
		}	
		System.out.println("---------------");
	
		//第三种:使用使用value获得值,并转为Collection集合,然后再使用forEach	
		//但这种只能得到value	
		 Map map2 = new HashMap(); 
		  map2.put(1,"a");
		  map2.put(2,"b"); 
		  map2.put(3,"c"); 
		  Collection<String> c = map2.values();
		 for(String value2:c) { 
		     System.out.println("值为  "+value2); 
		  }
		
	}
}		
``![在这里插入图片描述](https://img-blog.csdnimg.cn/20200518004752756.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0phdmVfbm9vYg==,size_16,color_FFFFFF,t_70#pic_center)

​	

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值