A Map is a data structure for storing key value pairs. It is similar to dictionaries in other programming languages. The Map interface defines operations on a map.
Java has a number of different Map implementations. HashMap is a commonly used one.
// Make an instance
Map<String, Integer> fruitPrices = new HashMap<>();
Add entries to the map using put.
fruitPrices.put("apple", 100);
fruitPrices.put("pear", 80);
// => { "apple" => 100, "pear" => 80 }
Only one value can be associated with each key.
Calling put
with the same key will update the key's value.
fruitPrices.put("pear", 40);
// => { "apple" => 100, "pear" => 40 }
Use get to get the value for a key.
fruitPrices.get("apple"); // => 100
Use containsKey to see if the map contains a particular key.
fruitPrices.containsKey("apple"); // => true
fruitPrices.containsKey("orange"); // => false
Remove entries with remove.
fruitPrices.put("plum", 90); // Add plum to map
fruitPrices.remove("plum"); // Removes plum from map
The size method returns the number of entries.
fruitPrices.size(); // Returns 2
You can use the keySet or values methods to obtain the keys or the values in a Map as a Set or collection respectively.
fruitPrices.keySet(); // Returns "apple" and "pear" in a set
fruitPrices.values(); // Returns 100 and 80, in a Collection
In this exercise you'll be writing code to manage a dictionary of international dialing codes using a Map
.
The dictionary allows looking up the name of a country (the map's value, as a String
) by the international dialing code (the map's key, as an Integer
),
Implement the method getCodes
that takes no parameters and returns a map of the dialing codes to country currently in the dictionary.
DialingCodes dialingCodes = new DialingCodes();
dialingCodes.getCodes();
// => empty map
The dictionary needs to be populated.
Implement the setDialingCode
method that takes a dialing code and the corresponding country and adds the dialing code and country.
If the dialing code is already in the map, update the map with the provided code and country.
DialingCodes dialingCodes = new DialingCodes();
dialingCodes.setDialingCode(679, "Unknown");
// => { 679 => "Unknown" }
dialingCodes.setDialingCode(679, "Fiji");
// => { 679 => "Fiji" }
Implement the getCountry
method that takes a map of dialing codes and a dialing code and returns the country name with the dialing code.
DialingCodes dialingCodes = new DialingCodes();
dialingCodes.setDialingCode(55, "Brazil");
dialingCodes.getCountry(55);
// => "Brazil"
When adding a dialing code, care needs to be taken to prevent a code or country to be added twice.
In situations where this happens, it can be assumed that the first entry is the right entry.
Implement the addNewDialingCode
method that adds an entry for the given dialing code and country.
However, unlike setDialingCode
, it does nothing if the dialing code or the country is already in the map.
DialingCodes dialingCodes = new DialingCodes();
dialingCodes.addNewDialingCode(32, "Belgium");
dialingCodes.addNewDialingCode(379, "Vatican City");
// => { 39 => "Italy", 379 => "Vatican City" }
dialingCodes.addNewDialingCode(32, "Other");
dialingCodes.addNewDialingCode(39, "Vatican City");
// => { 32 => "Belgium", 379 => "Vatican City" }
Its rare, but mistakes can be made.
To correct the mistake, we will need to know what dialing code the country is currently mapped to.
To find which dialing code needs to be corrected, implement the findDialingCode
method that takes in a map of dialing codes and a country and returns the country's dialing code.
Return null
if the country is not in the map.
DialingCodes dialingCodes = new DialingCodes();
dialingCodes.addDialingCode(44, "UK");
dialingCodes.findDialingCode("UK");
// => 44
dialingCodes.findDialingCode("Unlisted");
// => null
Now that we know which dialing code needs to be corrected, we proceed to update the code.
Implement the updateCountryDialingCode
method that takes the country's new dialing code and the country's name and updates accordingly.
Do nothing if the country is not in the map (as this method is meant to only help correct mistakes).
DialingCodes dialingCodes = new DialingCodes();
dialingCodes.addDialingCode(88, "Japan");
// => { 88 => "Japan" }
dialingCodes.updateCountryDialingCode(81, "Japan");
// => { 81 => "Japan" }
dialingCodes.updateCountryDialingCode(32, "Mars");
// => { 81 => "Japan"}
Sign up to Exercism to learn and master Java with 26 concepts, 149 exercises, and real human mentoring, all for free.