Map用法

package src;




import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

public class MapTest {
    //map的用法
    //map的遍历

//    public static void Print(Map<Integer,String> map){
//        for (Map.Entry<Integer,String> entry:map.entrySet()) {
//            System.out.println(entry.getKey()+"-"+entry.getValue());
//        }
//    }
    public static <K,V> void Print(Map<K,V> map){
        for (Map.Entry<K,V> entry:map.entrySet()) {
            System.out.println(entry.getKey()+"-"+entry.getValue());
        }
    }
    //1.定义和初始化
    public static void test1(){
        //first
        Map<Integer,String> map=new HashMap<>();
        map.put(1,"hello");
        map.put(2,"world");
        Print(map);
        System.out.println("---------------");
        //two
        Map<Integer,String>map1=new HashMap<>(map);
        Print(map1);
        System.out.println("---------------");
        map.put(2,"go");
        Print(map);
        Print(map1);
        //通过对象初始化是深拷贝实现
        System.out.println("---------------");
        //three
        Map map2=new TreeMap(map);
        Print(map2);
        System.out.println("---------------");
        //four
        Map map3=new HashMap<Integer,String>(){
            {
                put(3,"sd");
                put(5,"sdfs");
            }
            };
        Print(map3);
        //five
        Map map4=new HashMap(){{put(6,"a");put(9,"d");}};
        Print(map4);
        System.out.println("---------------");
        Map map5=new TreeMap(){{put(9,"r");}};
        Print(map5);
        System.out.println("---------------");
        //six
        Map map6=new LinkedHashMap();
        map6.put("a1","b1");
        Print(map6);
        System.out.println("---------------");
        //seven
        Map map7=Map.of(1,1,2,2,3,4);
        Print(map7);
    }
    public static void test2(){
        //2.添加、删除
        //put,新增一个键值对,如果存在,则更新键值对
        //remove ,删除键值对
        //isempty map是否为空
        //containkey 是否存在元素
        Map map=new HashMap<Integer,String>();
        for (int i = 1; i < 20; i++) {
            map.put(i,"a"+i);
        }
        map.remove(5);//key=5的元素被删除
        Print(map);
        System.out.println("--------------------");
        map.remove(3,"a3");//元素被删除
        System.out.println("--------------------");
        map.remove(4,"s");//该元素不存,故未删除成功
        System.out.println("--------------------");
        if(map.containsKey(8)) System.out.println("contain key=8 comment");else System.out.println("no exsit key=8 comment");
        System.out.println("--------------------");
        map.replace(10,"10");//key=10的元素被修改
        Print(map);
//        map.clear();
        if(map.isEmpty()) System.out.println("map is empty");else System.out.println("map is not empty");
        System.out.println("--------------------");
        //getOrDefault 存在返回Value,不存在返回默认值
        System.out.println(map.getOrDefault(15,"hello"));
        System.out.println(map.getOrDefault(30,"hello"));
        if(map.containsValue("a18")) System.out.println("contain value=a18");else System.out.println("not contain value=a18");
        System.out.println("--------------------");
        //map的foreach遍历
        map.forEach((a,b)->{
            System.out.println(a+"-"+b);
        });
        System.out.println("--------------------");
        //遍历key
        for (Object a:map.keySet()){
            System.out.println(a);
        }
        System.out.println("--------------------");
        //遍历value
        for (Object a:map.values()) System.out.println(a);
        //size 键值对数量
        System.out.println(map.size());
        System.out.println("--------------------");
    }
    
    public static void main(String[] args){
        test2();
    }
}

  

posted @ 2024-03-28 16:40  dmfsimle  阅读(11)  评论(0)    收藏  举报