Map
is an interface called java.util.Map, which is a data structure used when you want to handle a set of keys and values in Java.
By using the Map interface
, data structures such as
HashMap and` `LinkedHashMap
can be handled in a unified manner.
In this article, I will write about how to use the HashMap class.
Create your own class to implement the Map interface, or use an implementation class like HashMap
or LinkedHashMap
.
You cannot register the same key more than once in the Map, and each key can only be mapped to one value.
■ ** Key order is not guaranteed ** ■ ** Key duplication is not allowed ** ■ ** Values can be duplicated **
The declaration of the HashMap class is described as follows. Data type on the right side can be omitted by type inference The value can have List, Set, Map, and the key can be a class.
Map<Key model name,Value type name>Object name= new HashMap<>();
Map<String, List> map = new HashMap<>(); //Specify List as the value
Map<String, Set> map = new HashMap<>(); //Specify Set as the value
Map<String, Map<Integer, Object>> map = new HashMap<>(); //Specify Map as the value
Map<Example>, List> map = new HashMap<>(); //Specify a class called Example as a key
public class Sample {
public static void main(String[] args) {
Map<String,String> animal = new HashMap<>();
animal.put("monkey", "monkey");
animal.put("dog", "dog");
animal.put("cat", "Cat");
}
}
If the key has already been mapped, it will be overwritten, and if there is no mapping, "null" will be returned.
public class Sample {
public static void main(String[] args) {
Map<String,String> animal = new HashMap<>();
animal.put("monkey", "monkey");
animal.put("dog", "dog");
animal.put("dog", "Cat");
System.out.println(animal.get("dog")); //Cat
System.out.println(animal.get("pig")); //null
}
}
public class Sample {
public static void main(String[] args) {
Map<String,String> animal = new HashMap<>();
animal.put("monkey", "monkey");
animal.put("dog", "dog");
animal.put("cat", "Cat");
System.out.println(animal.get("cat")); //Cat
}
}
You can get all the Map keys by using the keySet method. Since the keySet method returns the Map key as a Set type, you can get all the keys by repeating the following using the extended for statement.
public class Sample {
public static void main(String[] args) {
Map<String,String> animal = new HashMap<>();
animal.put("monkey", "monkey");
animal.put("dog", "dog");
animal.put("cat", "Cat");
for (String pet : animal.keySet()) {
System.out.println(pet);// monkey dog cat
}
}
}
You can get all the values of Map by using the values method. Since the values method also returns the Map key as a Set type, you can get all the values like the keySet method by using the extended for statement.
public class Sample {
public static void main(String[] args) {
Map<String,String> animal = new HashMap<>();
animal.put("monkey", "monkey");
animal.put("dog", "dog");
animal.put("cat", "Cat");
for (String pet : animal.values()) {
System.out.println(pet);//Monkey dog cat
}
}
}
public class Sample {
public static void main(String[] args) {
Map<String,String> animal = new HashMap<>();
animal.put("monkey", "monkey");
animal.put("dog", "dog");
animal.put("cat", "Cat");
System.out.println(animal.size()); // 3
}
}
Use the containsKey method to determine if a particular key is included in the map.
public class Sample {
public static void main(String[] args) {
Map<String,String> animal = new HashMap<>();
animal.put("monkey", "monkey");
animal.put("dog", "dog");
animal.put("cat", "Cat");
if (animal.containsKey("dog")) { // true
System.out.println("Includes dog"); // Includes dog
}
}
}
public class Sample {
public static void main(String[] args) {
Map<String,String> animal = new HashMap<>();
animal.put("monkey", "monkey");
animal.put("dog", "dog");
animal.put("cat", "Cat");
if (animal.containsValue("monkey")) { // true
System.out.println("Contains monkey"); // Contains monkey
}
}
}
public class Sample {
public static void main(String[] args) {
Map<String,String> animal = new HashMap<>();
animal.put("monkey", "monkey");
animal.put("dog", "dog");
animal.put("cat", "Cat");
animal.clear();
if (animal.isEmpty()) { // true
System.out.println("It's empty"); // It's empty
}
}
}
public class Sample {
public static void main(String[] args) {
Map<String,String> animal = new HashMap<>();
animal.put("monkey", "monkey");
animal.put("dog", "dog");
animal.put("cat", "Cat");
animal.forEach((key,value) -> System.out.println(key + " " + value)));
//monkey monkey
//dog dog
//cat cat
}
}
Java Platform SE 8 Interface Map (https://docs.oracle.com/javase/jp/8/docs/api/java/util/Map.html)
Recommended Posts