Java集合(七)、Map集合 HashMap(常用)方法

HashMap的存值

复制代码
import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(1,"Jeo");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map);
    }
}
复制代码

注:添加重复的键会被最新添加的覆盖

 

HashMap的取值

复制代码
import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.get(3));
        System.out.println(map.get("Sezzy"));
    }
}
复制代码

 

HashMap的判断为空

复制代码
import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        System.out.println(map.isEmpty());
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.isEmpty());
    }
}
复制代码

 

HashMap判断是否含有key

复制代码
import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.containsKey(4));
        System.out.println(map.containsKey(2));
    }
}
复制代码

 

 HashMap判断是否含有value

复制代码
import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.containsValue("wang"));
        System.out.println(map.containsValue("Sezzy"));
    }
}
复制代码

 

HashMap删除这个key下的value

复制代码
import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.remove(2));
        System.out.println(map);
    }
}
复制代码

 

HashMap显示所有的value值

复制代码
import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.values());
        System.out.println(map);
    }
}
复制代码

 

HashMap的元素个数

复制代码
import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        System.out.println(map.size());
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.size());
    }
}
复制代码

 

HashMap取出全部的key

复制代码
import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        System.out.println(map.keySet());
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        System.out.println(map.keySet());
        map.put(3,"Pit");
        System.out.println(map.keySet());
        System.out.println(map);
    }
}
复制代码

 

HashMap替换key的value

复制代码
import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map);
        System.out.println(map.replace(3, "Jack"));
        System.out.println(map);
    }
}
复制代码

 

清空这个HashMap

复制代码
import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map);
        map.clear();
        System.out.println(map);
    }
}
复制代码

 

HashMap的克隆

复制代码
import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.clone());
        Object clone = map.clone();
        System.out.println(clone);
    }
}
复制代码

 

如果当前 Map 不存在键 key 或者该 key 关联的值为 null,那么就执行 put(key, value);否则,便不执行 put 操作:(java8新增方法)

复制代码
import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(maps);
        System.out.println(map.putIfAbsent(1, "wang"));
        System.out.println(map.putIfAbsent(4, "Lee"));
        System.out.println(map);
    }
}
复制代码

 

如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)compute 方法更适用于更新 key 关联的 value 时,新值依赖于旧值的情况 

复制代码
import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map);
        map.compute(2, (k, v) -> v == null ? "Suzan" : "Faker");
        System.out.println(map);
    }
}
复制代码

posted @ 2020-04-11 17:02  代码缔造的帝国  阅读(175)  评论(0编辑  收藏  举报