Map Interface in Java

Before proceeding with this article, If you are a new reader and you are not much familiar about collections. Read this article for more information ( Collections in java). Following are the key points to remember about Map and HashMap in java.

Maps keys to values and can’t contain duplicate keys

Each key map at least one value. There is a one to many relationship between keys and values

HashMap is an efficient implementation of Map interface

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

packageExamples;

import java.util.HashMap;

import java.util.Map;

publicclassMapEx{

publicstaticvoidmain(String[]args){

// keys are Integers

// Values are of type String

Map&lt;Integer,String&gt;map=newHashMap&lt;&gt;();

//add values in the map with a unique key

map.put(2,"C#");

map.put(3,"Java");

map.put(4,"JavaScript");

map.put(5,"C++");

// write to command line

map.forEach((key,val)-&gt;System.out.printf("%s %s%n",key,val));

// add and remove from the map

map.put(1,"PHP");

map.put(6,"Jquery");

map.remove(1);

// write again to command line

map.forEach((key,val)-&gt;System.out.printf("%s %s%n",key,val));

}

}

One can convert May keys into arrays with the following code.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

packageExamples;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

publicclassMapEx{

publicstaticvoidmain(String[]args){

// keys are Integers

// Values are of type String

Map&lt;Integer,String&gt;map=newHashMap&lt;&gt;();

//add values in the map with a unique key

map.put(2,"C#");

map.put(3,"Java");

map.put(4,"JavaScript");

map.put(5,"C++");

// write to command line

map.forEach((key,val)-&gt;System.out.printf("%s %s%n",key,val));

// add and remove from the map

map.put(1,"PHP");

map.put(6,"Jquery");

map.remove(1);

// write again to command line

map.forEach((key,val)-&gt;System.out.printf("%s %s%n",key,val));

//Convert Map keys to array

Integer[]keys=map.keySet().toArray(newInteger[map.keySet().size()]);

for(Integervals:keys){

System.out.println(vals);

}

//Convert Map keys to an ArrayList

List list=newArrayList(map.keySet());

for(Integervals:list){

System.out.println(vals);

}

}

}

Alternatively one can use lambda expressions to retrieve values using foreach method. Another method getOrDefault() is also available to retrieve the current value and if the value is not available it will return a default value. Following example demonstrates how to use lambda expression with foreach method and getOrDegault method. It is available in java 8 onward.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

packageExamples;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

publicclassMapEx{

publicstaticvoidmain(String[]args){

// keys are Integers

// Values are of type String

Map&lt;Integer,String&gt;map=newHashMap&lt;&gt;();

//add values in the map with a unique key

map.put(2,"C#");

map.put(3,"Java");

map.put(4,"JavaScript");

map.put(5,"C++");

// write to command line

map.forEach((key,val)-&gt;System.out.printf("%s %s%n",key,val));

// add and remove from the map

map.put(1,"PHP");

map.put(6,"Jquery");

map.remove(1);

// write again to command line

map.forEach((key,val)-&gt;System.out.printf("%s %s%n",key,val));

//use getOrDefault method

map.put(10,map.getOrDefault(1,"PHP"));

//Convert Map keys to array

Integer[]keys=map.keySet().toArray(newInteger[map.keySet().size()]);

// write to command line using lambda expression

map.forEach((key,value)-&gt;System.out.printf("%s %s%n",key,value));

//Convert Map keys to an ArrayList

List list=newArrayList(map.keySet());

for(Integervals:list){

System.out.println(vals);

}

}

}

There is an additional method can be used to calculate a value if it is absent in current map.