Java Map Interface

Posted in Java Collections By Raj On November 18, 2017

 

If you want to store key-value pair as an entry then I would recommend you go for map interface, in map the key must be unique, you can also have duplicate key’s but having duplicate key will not throw any compile time or runtime error, instead of that the old value will be replaced with new value

Some maps supports storing null values as key but it is not recommended ex: Hashmap and LinkedHashMap support’s storing null values as a key but the TreeMap does not support storing null values

The map does not come under collection framework because the collections store only one object but the map stores key-value pair as an entry

You can get the value by simply passing a key to the get() method of map object, if you want to store any value you can use put() method, if you want to read an entry from the map, you need to use Map.Entry interface

Java Map Interface

Check below examples on HashMap, in the output, the “author4” is not displaying instead of that “author5” is displaying

To read an entry I am using Map.Entry interface

 

Output :