Primary Menu

LinkedHashMap Tutorial Java – Complete Programming Example

In this chapter you will learn:

What is LinkedHashMap in Java?

How to iterate through LinkedHashMap?

Programming Example

What is LinkedHashMap class in Java?

LinkedHashMap is a combination of HashTable and Linked List and it stores items based on Key/Value pair. LinkedHashMap doesn’t allow inserting duplicate element. Point about LinkedHashMap are listed below:

It contains values based on key.

It contains only unique elements.

It maintains insertion order as the keys were inserted.

It is same as HashMap instead maintains insertion order.

It maintains Doubly Linked List running through of all its objects.

How to Initialize LinkedHashMap?

1

LinkedHashMap<Key,Value>map=newLinkedHashMap<Key,Value>();

Methods and Properties

Constructor

Description

LinkedHashMap()

It is used to construct a default LinkedHashMap.

LinkedHashMap(int capacity)

It is used to initialize a LinkedHashMap with the given capacity.

LinkedHashMap(int capacity, float fillRatio)

It is used to initialize both the capacity and the fillRatio.

LinkedHashMap(Map m)

It is used to initialize the LinkedHashMap with the elements from the given Map class m.

Methods

Methods

Description

Object get(Object key)

It is used to return the value to which this map maps the specified key.

void clear()

It is used to remove all mappings from this map.

boolean containsKey(Object key)

It is used to return true if this map maps one or more keys to the specified value.

Programming Example

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

importjava.util.*;

publicclassLinkedHashMap_Exmpl

{

publicstaticvoidmain(String[]args)

{

LinkedHashMap<Integer,String>Lmap=newLinkedHashMap<Integer,String>();

Lmap.put(22,"Jack");

Lmap.put(23,"Steven");

Lmap.put(26,"Clark");

Lmap.put(18,"Mathew");

System.out.println("First Methods Iteration");

PrintMethod1(Lmap);

System.out.println("Second Methods Iteration");

PrintMethod2(Lmap);

System.out.println("Direct Printing");

System.out.println(Lmap);

//Remove an element

System.out.println("Item removed : "+Lmap.remove(26));

//New Item added

System.out.println("Item Added : "+Lmap.put(19,"Sarah"));

System.out.println(Lmap);

}

//Iterate Through LinkedHashMap Method 1

publicstaticvoidPrintMethod1(LinkedHashMap<Integer,String>lm)

{

Set set=lm.entrySet();

Iterator itr=set.iterator();

while(itr.hasNext())

{

Map.Entry me=(Map.Entry)itr.next();

System.out.println("Key is : "+me.getKey()+" and Value is : "+me.getValue());

Summary

In this chapter I’ve tried to explain LinkedHashMap with complete programming example in java. Almost the programming of all the collections classes are same and there is not too much differences. Once, you learn to write collection programming It gets very easy to learn rest of the collection objects. In the next chapter you will learn TreeMap class in Java.