Map Interface using Java

Java Map - javatpoint


Represents a mapping between a key and a value.
Eg:-Key        Value
        😜          "Jhone"
        😁           "Ann"
        😜            "Leo"
If you  want to get any Value you must know key regards to value.

There are 3 types of  maps in Map interface in java,

        1.HashMap-Store data random manner,Minimum time  time complexity.Speed map compare to the         other MAPs. 

import java.util.HashMap;
import java.util.Map;
    public static void main(String[] args) {
Map<Integer,String> myMap=new HashMap<>();//create Map type HashMap object

        myMap.put(10,"Jhone");//key,value
        myMap.put(20,"Smith");
        myMap.put(30,"Ana");

        //Key stored & show in random manner
        System.out.println(myMap);

        //Jhone
        System.out.println(myMap.get(10));
        myMap.put(10,"Lisa");

        //Lisa
        System.out.println(myMap.get(10));

//Replace lastest updated value ignore privious one(Jhone) 
     
    }
}

        2.Linked HashMap-Store data in insertion order

public class LinkedHashMapDemo {
    public static void main(String[] args) {
        
        Map<String,Integer> myMap=new LinkedHashMap<>();

        myMap.put("Zara",50);
        myMap.put("Lisa",28);
        myMap.put("Jhon",38);
        myMap.put("Kumar",44);

        System.out.println( myMap);

        Map<Integer,String> myMap2=new LinkedHashMap<>();
        myMap2.put(50,"Zara");
        myMap2.put(20,"Lisa");
        myMap2.put(30,"Jhone");
        myMap2.put(40,"Kumar");

        System.out.println( myMap2);
    }
}
        3.Tree Map-Stored data in Ascending order.

import java.util.Map;
import java.util.TreeMap;

public class TreeMapDemo {
    public static void main(String[] args) {
          
            Map<String,Integer> myMap=new TreeMap<>();
            myMap.put("Zara",50);
            myMap.put("Lisa",28);
            myMap.put("Jhone",38);
            myMap.put("Kumari",44);

            System.out.println( myMap);

            Map<Integer,String> myMap2=new TreeMap<>();
            myMap2.put(50,"Zara");
            myMap2.put(20,"Lisa");
            myMap2.put(30,"Jhone");
            myMap2.put(40,"Kumar");

            System.out.println( myMap2);
        }
    }

    




Comments

Post a Comment

Popular posts from this blog

Hibernate-2

Hibernate-1

GIT & Version Controlling