Stored value of the 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); } }
Note: add a duplicate key will be added to cover the latest
HashMap values
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")); } }
Blank determination 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()); } }
Determining whether the key containing 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.containsKey(4)); System.out.println(map.containsKey(2)); } }
Determining whether the value contained 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.containsValue("wang")); System.out.println(map.containsValue("Sezzy")); } }
HashMap delete this value at 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.remove(2)); System.out.println(map); } }
HashMap show the value of all values
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); } }
The number of elements 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()); } }
Remove all of the key HashMap
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 replace the 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); } }
Clear this 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 clone
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); } }
If the current Map
key does not exist key or the key associated value null
, then the execution put(key, value)
; otherwise, it will not perform the put
operation: (java8 new method)
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); } }
If the current Map
is the value when the value is otherwise xx xx xx: (java8 new method) compute
method is available to update key associated value , the new value of the case depends on the value of old
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); } }