Java has many predefined Map implementations, with correspondingly different names and purposes. Two examples of such predefined maps are HashMap and TreeMap. HashMap is a commonly used map, and the HashMap has the getOrDefault() method.
The predefined HashMap is close to a general-purpose map and can be used for that purpose. Today, it is possible to have a general-purpose map object without defining any method, as explained below. Still, such instantiated general-purpose maps are not modifiable in 2022. This means that the length cannot be increased or decreased; no key or value can be changed. Thus, the content can be read but not changed. Since the getOrDefault() method for the map does not have to modify the map, the general-purpose map is employed in this article. If the programmer wants a modifiable map, then HashMap should be used.
The following is a set of fruit names and their outer colors:
"grape" => "pink"
"apricot" => "orange"
"passion fruit" => "purple"
"banana" => "yellow"
This is a mapping of fruit names to according color on paper. A map consists of key/value pairs. So, each fruit name is a key, and its corresponding color is its value. For this mapping, if “watermelon” is chosen as a key, the value “green” should be returned; if “grape” is chosen, the value “pink” should be returned; if “apricot” is chosen, the value, “orange” should be returned; and so on.
The programmer or the user can decide that the default color is red. This means that if a key (fruit name) such as “blackberry”, which is not in the list, is chosen, then red will have to be returned.
One of the methods for the Map Interface is getOrDefault(), whose meaning is given below:
The getOrDefault() Syntax
This method for the map returns a copy of the value corresponding to the key. If there is no such key in the map, the default value chosen by the programmer or the user will be returned. The full syntax of the method is:
If the key exists in the map, the value corresponding to the key will be returned. The key is the first argument of this method. If the key does not exist, the default value will be returned. The default value is the second argument. It can be put there, either by the programmer or by the user. Code sample of the use of this method is shown below.
Key/Value Pair
The key/value pair objects, can be created first to have a general purpose. After that, they are included into the map. There is a class called, AbstractMap.SimpleEntry in the java.util.* package. The object of this class is a key/value pair, as shown in the following short program:
public class TheClass {
public static void main(String[] args) {
AbstractMap.SimpleEntry<String, String> pair1 = new AbstractMap.SimpleEntry<String, String>("watermelon", "green");
AbstractMap.SimpleEntry<String, String> pair2 = new AbstractMap.SimpleEntry<String, String>("grape", "pink");
AbstractMap.SimpleEntry<String, String> pair3 = new AbstractMap.SimpleEntry<String, String>("apricot", "orange");
AbstractMap.SimpleEntry<String, String> pair4 = new AbstractMap.SimpleEntry<String, String>("passion fruit", "purple");
AbstractMap.SimpleEntry<String, String> pair5 = new AbstractMap.SimpleEntry<String, String>("banana", "yellow");
System.out.println();
}
}
These are key/value pairs in code form for the previous map data of fruits and their colors. Note the importation of the java.util.* package.
General Purpose Map
After the previous code, the general purpose map can be produced with the following statement:
The syntax for the Map.ofEntries() static method is:
Where K stands for key and V stands for value. It is in the java.util.* package, which has to be imported. A package is imported once only, for more than one of its components, for the same program.
Using the getOrDefault() Method
The following program shows how the getOrDefault() method is used:
public class TheClass {
public static void main(String[] args) {
AbstractMap.SimpleEntry<String, String> pair1 = new AbstractMap.SimpleEntry<String, String>(“watermelon", "green");
AbstractMap.SimpleEntry<String, String> pair2 = new AbstractMap.SimpleEntry<String, String>("grape", "pink");
AbstractMap.SimpleEntry<String, String> pair3 = new AbstractMap.SimpleEntry<String, String>("apricot", "orange");
AbstractMap.SimpleEntry<String, String> pair4 = new AbstractMap.SimpleEntry<String, String>("passion fruit", "purple");
AbstractMap.SimpleEntry<String, String> pair5 = new AbstractMap.SimpleEntry<String, String>("banana", "yellow");
Map<String, String> fruitMap = Map.ofEntries(pair1, pair2, pair3, pair4, pair5);
String valueG = fruitMap.getOrDefault("grape", "red"); //method of interest
System.out.println(valueG);
}
}
The output is:
The key, “grape”, was present. So its corresponding value was returned.
In the following program, the key, lemon, is not present in the map, so the default value (second argument) typed in by the programmer is returned:
public class TheClass {
public static void main(String[] args) {
AbstractMap.SimpleEntry<String, String> pair1 = new AbstractMap.SimpleEntry<String, String>("watermelon", "green");
AbstractMap.SimpleEntry<String, String> pair2 = new AbstractMap.SimpleEntry<String, String>("grape", "pink");
AbstractMap.SimpleEntry<String, String> pair3 = new AbstractMap.SimpleEntry<String, String>("apricot", "orange");
AbstractMap.SimpleEntry<String, String> pair4 = new AbstractMap.SimpleEntry<String, String>("passion fruit", "purple");
AbstractMap.SimpleEntry<String, String> pair5 = new AbstractMap.SimpleEntry<String, String>("banana", "yellow");
Map<String, String> fruitMap = Map.ofEntries(pair1, pair2, pair3, pair4, pair5);
String valueG = fruitMap.getOrDefault("lemon", "red"); //method of interest
System.out.println(valueG);
}
}
The output is:
“red” was coded using the getOrDefault() method by the programmer. Any other value could have been typed in.
Remember, the getOrDefault() method is a method of Interface Map. It works with a general purpose map and other Java predefined maps.
Conclusion
The getOrDefault() method returns the value that corresponds to a key in a map. If there is no such key in the map, then a default value should be returned. The first argument of this method is the key whose corresponding value is needed. The second argument is the default value, inputted by the programmer or the user. The method has only these two arguments. The method works with a general purpose map and other Java predefined maps. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.