Map in Java? Confusion about it can get clear here!


What is Map in Java?




Hello guys!

Hope you guys doing well in your learning path. Today i'm going to explain you about topic from java which is always been little bit tricky for beginner.
Name of topic is Map in Java.



So let's start with few important and basic thing we should take under consideration about it which will make your understanding more easy about it.
  • First of all Map is not a part of collection framework or package but it is used along with it because it resides in completely different package reside in the same directory where collection framework is present. So always remember this point.
  • Map is not child of collection interface its follow separate hierarchy.
  • Another important point is it allows heterogeneous objects which means your not force to use only same kind of data you can store any type of data.
  • Mainly Map is used when we want to store group of key value pair.
  • It is kind of a associative array.
  • When we think about using Map data structure then key and value both must be object.
  • You can enter same Value for multiple entries but for every entry you add its key has to be unique.
  • Map interface has one inner interface called Entry (Map.Entry).
  • A Map entry is also a key value pair.
  • It has few important method like:-
    • public object getKey():- is used to get key.
    • public object getValue():- is used to get value.
    • public object put(object key, object value):- is used to put one entry of map in the form of key value.
    • public object putAll(Map map):- is used to put one map  object.
    • There are many other useful methods of map you can use.
  • It have one child interface called SortedMap.
    • whenever you wanted to sort key value pair group you can use this interface.
    • It applies sorting logic on key.
    • It has one child interface called NavigableMap.
      • Which have several utility methods provide to navigate in sortedMap.
      • It is implemented by TreeMap Class.
        • It's similar to TreeSet.
        • It is not thread safe.
        • No duplicate entry allow when you using this class.
        • It perform natural sorting and we can achieve custom sorting using comparator .
  • Map interface implemented by many classes which are mentioned below:-
    • HashMap:-
      • It contains value based on key.
      • Hashtable data structure is used.
      • It can contain only unique element.
      • It can allow one null key and multiple null values.
      • It follows natural sorting.
      • HashMap has one child class called LinkedHashMap( Hybrid data structure).
        • It uses Hash-Table as well as doubly linked list.
        • It preserve Insertion order.
        • But it provide slower insertion and deletion.
    • HashTable:-
      • This is another class Map interface have as implementing child class.
      • This Class is a Thread safe version of HashMap.
      • HashTable provide Unsorted and Unordered contents you stored it it.
      • HashTable doesn't allow null value unlike HashMap this is very important point to remember.

So This is what I'm covering today for this topic i hope you will get some help from it for improvising your understanding about Map interface.

Let me know if you want some additional information about it; I'm here to help you.

Comments

Popular Posts