Let's learn java programming language with easy steps

Thursday, 8 June 2017

LinkedHashMap In Java

 Java LinkedHashMap Class

LinkedHashMap is a class in java collection framework and it is available in java.util.* package. LinkedHashMap class implements Map interface and extends(inherit) HashMap class.

LinkedHashMap class contains key & value pairs.

This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries.




Some Points About LinkedHashMap Class

  • LinkedHashMap class implements Map interface and extends HashMap class.
  • LinkedHashMap class contains values on the basis of key i.e it contains key & value pairs.
  • LinkedHashMap class contains only unique elements, It doesn't contains duplicate elements.
  • LinkedHashMap class maintains insertion order of an element.
  • LinkedHashMap class have one null key and can have multiple null values.


Hierarchy of LinkedHashMap Class

LinkedHashMap in Java
In the above picture, LinkedHashMap class extends HashMap class and HashMap class extends AbstractMap class and AbstractMap class implements top most interface Map interface.


Constructors of LinkedHashMap Class

There are some constructors of linked hash map class.

1) LinkedHashMap()

It constructs an empty insertion-ordered LinkedHashMap instance with the default initial capacity(16) and load factor(0.75).


2) LinkedHashMap(int initialCapacity)

It constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and default load factor(0.75).

3) LinkedHashMap(int initialCapacity, float loadFactor)

It constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and load factor.

4) LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)

It constructs an empty LinkedHashMap instance with the specified initial capacity , load factor and ordering mode.

5) LinkedHashMap(Map m)

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


Methods of LinkedHashMap Class

There are some methods of linked hash map class in java.

1) void clear()

This method removes all mapping from this map.

2) boolean containsValue(Object value)

This method returns true if this map maps one or more keys to the specified value.

3) Set entrySet()

This method returns a Set view of the mappings contained in this map.

4) Set keySet()

This method returns a Set view of the keys contained in this map.

5) Object get(Object key)

This method returns the value to which this map maps the specified key.

6) protected boolean removeEldestEntry(Map.Entry eldest)

Returns true if this map should remove its eldest entry.


Java LinkedHashMap Example

import java.util.*;
class LinkedHashMapExample
{
public static void main(String args[])
{
//creating linked hash map
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>();
lhm.put(500, "mango");//adding elements
lhm.put(100, "banana");
lhm.put(700, "apple");
lhm.put(300, "orange");
//traverse elements
for(Map.Entry me : lhm.entrySet())
{
System.out.println(me.getKey()+" "+me.getValue());
}
}
}

output : 500 mango
              100 banana
              700 apple
              300 orange

Q. What is the difference between LinkedHashMap and HashMap Class ?

Ans.  LinkedHashMap class : Maintains insertion order of elements .

HashMap class : Doesn't maintain any insertion order of an element.

Q. What is the difference between LinkedHashMap and LinkedHashSet Class ?

Ans.  LinkedHashMap : Contains key and value pairs.

for example : lhm.put(1, "apple");

LinkedHashSet : Contains only value.

for example : lhs("apple");

Q. What is similarities between LinkedHashMap and LinkedHashSet class ?

Ans. The similarities between LinkedHashMap and LinkedHashSet class is that both maintains insertion order of an element.

Read More:

Java Collection Framework Interview Questions.
Java Collections Class.


Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate