java语言 Map集合 的常用的最最基本的功能,其他方法还在学习中,有待更新......

1,添加功能
2,删除功能
3,判断功能
.
.
.
package map;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Map1 {

public static void main(String[] args) {
    //add1(); 
    //remove();
    //is();
    Map<String,Integer> map=new HashMap<>();
    map.put("gaga",11);
    map.put("wwww",22);
    map.put("eeee",33);
    map.put("tttt",44);

    Collection<Integer> c=map.values();  //获取集合中的所有值
    System.out.println(c);              //[11, 44, 22, 33]
    System.out.println(map.size());   //返回集合中键值对的个数 4
}

private static void is() {
    //判断功能
    Map<String,Integer> map=new HashMap<>();
    map.put("gaga",11);
    map.put("wwww",22);
    map.put("eeee",33);
    map.put("tttt",44);

    System.out.println(map.containsKey("wwww"));  //是否包含键,包含就返回true,否则就返回false
    System.out.println(map.containsValue(33));  //是否包含值,包含就返回true,否则就返回false
}

private static void remove() {
    //删除功能
    Map<String,Integer> map=new HashMap<>();
    map.put("gaga",11);
    map.put("wwww",22);
    map.put("eeee",33);
    map.put("tttt",44);

    Integer value=map.remove("wwww");          //根据键删除元素,返回对应的值
    System.out.println(value);                 //输出22
    System.out.println(map);                   //输出删除后的结果{gaga=11, tttt=44, eeee=33}
}

private static void add1() {
    Map<String,Integer> map=new HashMap<>();
    //添加功能
    Integer i1=map.put("张三",23);
    Integer i2=map.put("李四",24);
    Integer i3=map.put("lala",25);
    Integer i4=map.put("haha",26);
    Integer i5=map.put("lala",20);             //相同的键不存储,值覆盖,把被覆盖的值返回
    System.out.println(map);                   //{李四=24, 张三=23, haha=26, lala=20}

    System.out.println(i1);
    System.out.println(i2);
    System.out.println(i3);
    System.out.println(i4);                    // 加上(Integer i=) 就输出null。
    System.out.println(i5);                     //输出lala=20,返回值为25
}

}

猜你喜欢

转载自blog.csdn.net/Mr_Ycy/article/details/81544797
今日推荐