Topics Index

Linked Hash Set

If you need explanation Read this topic

If you need Answer Take test on this topic

Linked Hash Set
What will be the output of the following program?
import java.util.*;

public class LinkedHashSet
    public static void main(String[] args)
        LinkedHashSet<Integer> lhashSet = new LinkedHashSet<Integer>();
        lhashSet.add(new Integer(1));
        lhashSet.add(new Integer(2));
        lhashSet.add(new Integer(3));
        lhashSet.add(new Integer(3));
        lhashSet.add(new Integer(5));
        lhashSet.add(new Integer(4));
        lhashSet.add(new Integer(5));
        lhashSet.add(new Integer(5));
        Object[] objArray = lhashSet.toArray();
        for (Object element : objArray)
            System.out.print(element + " ");

A. 1 2 3 5 4
B. 1 2 3 3 4 5 5 5
C. 1 2 3 3 5 4 5 5
D. Compilation Error 
E. Runtime Error
Topic: Java LinkedHashSet

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is 1 2 3 5 4

here we  have the LinkedHashSet class which extends the HashSet class and this class also implements ithe Set interface, all the set interfaces classes wont allow the duplicates and LinkedHashSet class maintains insertion order
here we create the LinkedHashSet  class which can store the Integer objects, than we add the some of the Integer objects like 1 2 3  3 5 4 5 5 as i said LinkedHashSet  class implements the Set interface so it wont allow the Duplicates so it will store only 1 2 3 5 4 and LinkedHashSet  class follow the insertion order so order will be 1 2 3 5 4, afrer 4 we add the 5 twice but 5 is already stored in the List so it wont store again

Posted by Uday Kumar    2015-03-06 15:50:00

There is Error in your options @Merit Campus

ANS IS 1 2 3 5 4

--Here the concept of "LikedHashSet" collection is used.

--Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation.)

--After that integers 1 2 3 5 4 are added in linkedhashset but there are duplicate elements in it.

--After that it is converted into object array by using toArray() method which removes the duplicate elements.

1 2 3 5 4

Posted by Mânïshå Mùlchåndânï    2015-03-07 09:56:00

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Mânïshå Mùlchåndânï, Sai Ram,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Cool Pawan. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-03-09 02:11:31

© meritcampus 2019

All Rights Reserved.

Open In App