package com.collection_.map_;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings({"all"})
public class MapMethod {
public static void main(String[] args) {
Map map = new HashMap();
//1. put 添加
map.put("邓超",new Book("西游记",100));
map.put("邓超","孙俪");//替换了
map.put("王宝强","马蓉");
map.put("宋喆","马蓉");
map.put("彭于晏",null);
map.put(null,"刘亦菲");
map.put("鹿晗","关晓彤");
System.out.println("map=" + map);
//2. remove 根据键删除映射关系
map.remove("宋喆");
System.out.println("map=" + map);
//3. get 根据键获取值(返回value)
Object val = map.get("鹿晗");
System.out.println("val=" + val);//val=关晓彤
//4. size 获取元素个数
System.out.println("k-v=" + map.size());//k-v=5
//5. isEmpty 判断个数是否为0
System.out.println(map.isEmpty());//false
//6. clear 清除
//map.clear();
System.out.println("map=" + map);//map={}
//7. containsKey 查找键是否存在 (输入 null 会返回 false)
System.out.println(map.containsKey("王宝强"));//ture
System.out.println(map.containsValue("刘亦菲"));//ture
}
}
class Book{
private String name;
private int num;
public Book(String name, int num) {
this.name = name;
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
Map方法
最新推荐文章于 2023-12-26 14:19:29 发布