Menu
Topics Index
...
`

HashMapDemo - Java Example Program


HashMapDemo
import java.util.*;

class HashMapDemo
{
    public static void main(String arg[])
    {
        HashMap<Character, Integer> asciicodemap = new HashMap<Character, Integer>();
        asciicodemap.put('A', 65);
        asciicodemap.put('B', 66);
        asciicodemap.put('C', 67);
        asciicodemap.put('D', 68);
        asciicodemap.keySet();
        System.out.println(asciicodemap.keySet());    
    }
}
OUTPUT

[D, A, B, C]

DESCRIPTION

We initially create a hash map with Character as type key and Integer as type value. We add the elements to the hash map asciicodemap by usingput(). Here the keyset method returns a Set view of the keys contained the map. Note that the output displayed is not in the order in which we entered into the map but in the order the iterator has read it.

THINGS TO TRY
  • Try for the below code.
    HashMap<Character, Integer> asciicodemap = new HashMap<Character, Integer>();
    asciicodemap.put('A', 65);
    asciicodemap.put('B', 66);
    System.out.println(asciicodemap.get('A'));
    The output for the above code will be 65. We can get individual key values by using get method.
  • Try to use all the methods defined by Map Interface.

© meritcampus 2019

All Rights Reserved.

Open In App