Map Basic operation II

Checking if the map contains a specified value:The method containsValue(Object value) returns true if the map contains one or more keys associated with the specified value. For example:

1
2
3
if (mapHttpErrors.containsValue("OK")) {
    System.out.println("Found status OK");
}

Output:

1
Found status OK

Removing a mapping from the map:The remove(Object key) method removes the mapping for a key from the map if it is present (we care about only the key, and the value does not matter). This method returns the value to which the map previously associated the key, or null if the map doesn’t contain mapping for the key. Here’s an example:

1
2
3
4
5
String removedValue = mapHttpErrors.remove(500);
if (removedValue != null) {
    System.out.println("Removed value: " + removedValue);
}

Output:

1
Removed value: Internal Server Error

Similarly, the remove(Object key, Object value) method removes the mapping of a specified key and specified value, and returns true if the value was removed. This method is useful in case we really care about the key and value to be removed. Recommended Book: Java Generics and Collections Replacing a value associated with a specified key:The replace(K key, V value) method replaces the entry for the specified key only if it is currently mapping to some value. This method returns the previous value associated with the specified key. Here’s an example:

1
2
3
4
5
System.out.println("Map before: " + mapHttpErrors);
mapHttpErrors.replace(304, "No Changes");
System.out.println("Map after: " + mapHttpErrors);

Output:

1
2
Map before: {400=Bad Request, 304=Not Modified, 200=OK, 301=Moved Permanently}
Map after: {400=Bad Request, 304=No Changes, 200=OK, 301=Moved Permanently}

Similarly, the replace(K key, V oldValue, V newValue) method replaces the entry for the specified key only if it is currently mapping to the specified value. This method returns true if the value was replaced. Useful in case we want to replace exactly a key-value mapping. Getting the size of the map:The size()method returns the number of key-value mappings in this map. For example:

1
int size = mapHttpErrors.size();

Output:

1
Number of HTTP status code: 5

Checking if the map is empty:The isEmpty() method returns true if the map contains no key-value mappings. For example:

1
2
3
4
5
if (mapHttpErrors.isEmpty()) {
    System.out.println("No Error");
} else {
    System.out.println("Have HTTP Errors");
}

Output:

1
Have HTTP Errors

 

 Iterating Over a Map (using Collection views)

As a Map is not a true collection, there is no direct method for iterating over a map. Instead, we can iterate over a map using its collection views. Any Map’s implementation has to provide the following three Collection view methods:

  • keySet(): returns a Set view of the keys contained in the map. Hence we can iterate over the keys of the map as shown in the following example:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    Map<String, String> mapCountryCodes = new HashMap<>();
    mapCountryCodes.put("1", "USA");
    mapCountryCodes.put("44", "United Kingdom");
    mapCountryCodes.put("33", "France");
    mapCountryCodes.put("81", "Japan");
    Set<String> setCodes = mapCountryCodes.keySet();
    Iterator<String> iterator = setCodes.iterator();
    while (iterator.hasNext()) {
        String code = iterator.next();
        String country = mapCountryCodes.get(code);
        System.out.println(code + " => " + country);
    }

    Output:

    1
    2
    3
    4
    44 => United Kingdom
    33 => France
    1 => USA
    81 => Japan
  • values(): returns a collection of values contained in the map. Thus we can iterate over values of the map like this:
    1
    2
    3
    4
    5
    Collection<String> countries = mapCountryCodes.values();
    for (String country : countries) {
        System.out.println(country);
    }

    Output:

    1
    2
    3
    4
    United Kingdom
    France
    USA
    Japan
  • entrySet(): returns a Set view of the mappings contained in this map. Therefore we can iterate over mappings in the map like this:
1
2
3
4
5
6
7
8
Set<Map.Entry<String, String>> entries = mapCountryCodes.entrySet();
for (Map.Entry<String, String> entry : entries) {
    String code = entry.getKey();
    String country = entry.getValue();
    System.out.println(code + " => " + country);
}

Output:

1
2
3
4
44 => United Kingdom
33 => France
1 => USA
81 => Japan

Since Java 8 with Lambda expressions and the forEach() statement, iterating over a Map is as easy as:

1
2
mapCountryCodes.forEach(
    (code, country) -> System.out.println(code + " => " + country));

Output:

1
2
3
4
44 => United Kingdom
33 => France
1 => USA
81 => Japan
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: