java——Map常用方法总结

package test1;

import java.util.Map;

import java.util.HashMap;

public class Test {
	public static void main(String[] args){

		 Map<String,Integer> scores = new HashMap<String,Integer>();//<String,Integer>,其中String用于限定key的数据类型,Integer显示value的数据类型,泛型不能使基本数据类型

		 //1、put()方法,添加数据
		 //Map集合key不允许重复,重复的话以最后一个为主
		 scores.put("Jim", 100);
		 scores.put("Jim", 60);
		
		 //2、get(key)方法获取key值   
		 int score = scores.get("Jim");
		 System.out.println(score);
		 
		 //3、size()方法用法与List相同  
		 int size = scores.size();
		 System.out.println(size);

		 //4、clear()方法用法与List相同
		 scores.clear();
		 size = scores.size();
		 System.out.println(size);
		 
		 //5、isEmpty()方法用法与List相同   
		 boolean flag = scores.isEmpty();
		 System.out.println(flag);
		    
		 scores.put("Jim", 100);
		 
		 //6、replace()方法,替换元素
		 scores.replace("Jim", 0);
		 score = scores.get("Jim");
		 System.out.println(score);		
	}
}

在这里插入图片描述

发布了29 篇原创文章 · 获赞 3 · 访问量 363

猜你喜欢

转载自blog.csdn.net/qq_44687512/article/details/105496013