Showing posts with label hashmap java. Show all posts
Showing posts with label hashmap java. Show all posts

Tuesday, 16 June 2015

How to get entry set from HashMap

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapEntrySet {
   
    public static void main(String[] args) {
       
    Map<Integer,String> hm = new HashMap<Integer,String>();
       
    /*
     * Associates the specified value with the specified key in
       this map (optional operation). If the map previously
       contained a mapping for the key, the old value is
       replaced by the specified value
     */
        hm.put(1,"ankush");
        hm.put(2, "amit");
        hm.put(3,"shivam");
        hm.put(4,"ankit");
        hm.put(5, "yogesh");
       
        //print hashmap
        System.out.println("HashMap = "+hm);
       
        Set<Entry<Integer, String>> entry =hm.entrySet();
       
        for (Entry<Integer, String> entry2 : entry) {
            System.out.println(" Key = "+entry2.getKey()+" Value = "+entry2.getValue());
        }
    }

}




Output:

 HashMap = {1=ankush, 2=amit, 3=shivam, 4=ankit, 5=yogesh}

 Key = 1 Value = ankush

 Key = 2 Value = amit
 
 Key = 3 Value = shivam

 Key = 4 Value = ankit

 Key = 5 Value = yogesh

Sunday, 14 June 2015

How to get size of HashMap

import java.util.HashMap;
import java.util.Map;

public class HashMapKeyCount {
   
    public static void main(String[] args) {
       
        Map<Integer,String> hm = new HashMap<Integer,String>();
           
        /*
         * Associates the specified value with the specified key in
           this map (optional operation). If the map previously
           contained a mapping for the key, the old value is
           replaced by the specified value
         */
            hm.put(1,"ankush");
            hm.put(2, "amit");
            hm.put(3,"shivam");
            hm.put(4,"ankit");
            hm.put(5, "yogesh");
           
            //Returns the number of key-value mappings in this map
           
            System.out.println("HashMap size = "+hm.size());
           
           
        }
   
}

Output:

HashMap size = 5

How to get all keys of HashMap

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashMapGetAllKeys {



        public static void main(String[] args) {
           
        Map<Integer,String> hm = new HashMap<Integer,String>();
           
        /*
         * Associates the specified value with the specified key in
           this map (optional operation). If the map previously
           contained a mapping for the key, the old value is
           replaced by the specified value
         */
            hm.put(1,"ankush");
            hm.put(2, "amit");
            hm.put(3,"shivam");
            hm.put(4,"ankit");
            hm.put(5, "yogesh");
           
            /*
             * Returns a Set view of the keys contained in this map
             */
           
            Set<Integer> keys =hm.keySet();
           
            for (Integer key : keys) {
                System.out.println(" key ="+key);
            }
        }
   
}

Output:

  key =1

  key =2

  key =3

  key =4
 
  key =5

How to Search Value in HashMap Java

import java.util.HashMap;
import java.util.Map;

public class HashMapSearchValue {

    public static void main(String[] args) {
      
        Map<Integer,String> hm = new HashMap<Integer,String>();
          
        /*
         * Associates the specified value with the specified key in
           this map (optional operation). If the map previously
           contained a mapping for the key, the old value is
           replaced by the specified value
         */
            hm.put(1,"ankush");
            hm.put(2, "amit");
            hm.put(3,"shivam");
            hm.put(4,"ankit");
            hm.put(5, "yogesh");
          
            System.out.println(" value john ="+hm.containsValue("john"));
            System.out.println(" value amit ="+hm.containsValue("amit"));
          
            System.out.println(" value piyush ="+hm.containsValue("piyush"));
            System.out.println(" value shivam ="+hm.containsValue("shivam"));
          
        }

}

Output;

 value john =false

 value amit =true

 value piyush =false

 value shivam =true

How to Search key in HashMap Java


import java.util.HashMap;
import java.util.Map;

public class HashMapSearchKey {
    public static void main(String[] args) {
       
    Map<Integer,String> hm = new HashMap<Integer,String>();
       
    /*
     * Associates the specified value with the specified key in
       this map (optional operation). If the map previously
       contained a mapping for the key, the old value is
       replaced by the specified value
     */
        hm.put(1,"ankush");
        hm.put(2, "amit");
        hm.put(3,"shivam");
        hm.put(4,"ankit");
        hm.put(5, "yogesh");
       
        /*
         * Returns true if this map contains a
         *  mapping for the specified key
         */
   
    System.out.println("key  1   ="+hm.containsKey(1));
    System.out.println("key  20  ="+hm.containsKey(20));
    System.out.println("key 9    ="+hm.containsKey(9));
    System.out.println("key  5   ="+ hm.containsKey(5));
       
    }
}


Output:
key  1   =true

key  20  =false

key 9    =false

key  5   =true


How to storing Java Object In HashMap

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashMapStoreObject {
   
    public static void main(String[] args) {
       
        Map<Integer,Emp> hm = new HashMap<Integer, Emp>();
       
        hm.put(1, new Emp(100, "dk") );
        hm.put(2, new Emp(102, "kapil") );
        hm.put(3, new Emp(105, "anup") );
        hm.put(4, new Emp(205, "kapil") );
        hm.put(5, new Emp(110, "pk") );
       
           Set<Integer> keys = hm.keySet();
      
           for (Integer key : keys) {
              
               Emp emp = hm.get(key);
            System.out.println(" Id = "+emp.getId()+" name = "+emp.getName());
        }
          
    }

}


class Emp{
   
        private int id;
        private String name;
       
        public Emp(int id, String name) {
            this.id = id;
            this.name = name;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
       
}




Output:

 Id = 100 name = dk

 Id = 102 name = kapil

 Id = 105 name = anup

 Id = 205 name = kapil

 Id = 110 name = pk


How to create HashMap In Java

import java.util.HashMap;
import java.util.Map;

public class CreateHashMap {

    public static void main(String[] args) {
       
    Map<Integer,String> map = new HashMap<Integer,String>();
       
    /*
     * Associates the specified value with the specified key in
       this map (optional operation). If the map previously
       contained a mapping for the key, the old value is
       replaced by the specified value
     */
        map.put(1,"ankush");
        map.put(2, "amit");
        map.put(3,"shivam");
        map.put(4,"ankit");
        map.put(5, "yogesh");
       
        //print hashmap
        System.out.println("HashMap = "+map);
       
       
    }
   
}




Output:


HashMap = {1=ankush, 2=amit, 3=shivam, 4=ankit, 5=yogesh}

Java HashMap Examples