Basic operations of "Map" interface in java

     ‘Map’ is an interface in java Collections Framework. It contains a pair of values which is a key associated with its value. Map has unique values for each key. One single entry is for one value.

Features of Map:

Map has unique features compared to other data structures like queue or Stack. The features are listed below.

  • Key and Values are paired.
  • It always include the key as unique. It does not have any duplicate keys. Sometimes, it allows duplicate value.
  • Null values are also acceptable. But a single null key is accepted due to implementation purpose.

Various ways of implementing “Map”:

              “Map” can be implemented as three ways.

  • 1.       HashMap: It may be in any order. I/O operations are in constant time performance.
  • 2.       LinkedHashMap: The insertion maintains an order.
  • 3.       TreeMap: It follows a sorted order based on the program. It uses “NavigableMap” interface.

Built in methods in Map interface:

It has the following list of methods.

1. entrySet():

          It gives you the mappings of the map interface.

          Eg:  Set<Map.Entry<String, String>> entries = map.entrySet();

2.keySet():

        This function lists out the keys in the map interface.

        Eg:  Set<String> key_map =map.keySet();

3.values():

       The values of each key is displayed.

       Eg: Collection<String> value_map = map.values();

4.get(Object key)

       It is the input statement to get the key value.

       Eg: String value_map = map.get("key1");

5. put(K key_map, V value_map):

      This function creates the association for key and value in the map.

      Eg: map.put("key12", "value12");

6. remove(Object key):

      This function deletes the key.

      Eg: remove(“key12”);

7. containsKey(Object key_map):

       It checks the key is available is not by returning true or false value.

       Eg: Boolean iscontain=map.ContainsKey(“key12”);

8.containsValue(Object value_map):

             This function checks the value associated for map is available or not.

 Eg:    boolean iscontains = map.containsValue("value12");

9.size():

       It returns the number of elements in the map. Here, the elements are the “key and value”.

       Eg int the_size = map.size();

10.isEmpty():

    This function gives you `true` if the map contains has no pair of key and value.

    Eg: Boolean isEmpty_or_not = map.isEmpty();

11.clear():

    This function deletes the entire mapping in the map interface.

    Eg: map.clear();

These are the basics of map interface and its built-in member functions in java is illustrated.

No comments:

Post a Comment