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); } }
原文地址:https://www.cnblogs.com/lixiansheng/p/11354623.html
时间: 2024-11-05 20:32:01