Let's learn java programming language with easy steps

Sunday, 21 May 2017

Java LinkedHashSet

 

Java LinkedHashSet Class

Java LinkedHashSet is a class in java collection. LinkedHashSet class implements set interface like HashSet class in Java. LinkedHashSet extends(inherit) HashSet class.


Some Important Points About LinkedHashSet Class

  • LinkedHashSet class doesn't contains duplicate values. It contains only unique values like HashSet class in java.
  • LinkedHashSet class maintains insertion order.
  • LinkedHashSet class in not synchronized.
  • LinkedHashSet class allows null values.
  • By using foreach loop and Iterator interface, We can retrieve an elements from LinkedHashSet class.

Hierarchy of LinkedHashSet Class

LinkedHashSet


Here in the above diagram, class LinkedHashSet extends HashSet class and HashSet class extends Abstract set class and AbstractSet class implements set interface.

Constructors of LinkedHashSet Class

There are some constructors of linkedhashset class in java. These are:

1) HashSet()

It makes a default HashSet.

2) HashSet(Collection c)

This constructor is used to initialize the hash set by using the elements of collection c.

3) LinkedHashSet(int capacity)

This constructor is used to initialize the hash set to the given integer value capacity.

4) LinkedHashSet(int capacity, float fillRatio)

This constructor is used to initialize both integer and float values.


Java LinkedHashSet Example

This is simple example of LinkedHashSet class in java collection framework.

import java.util.*;
class LinkedHashSetExample
{
public static void main(String args[])
{
//creating LinkedHashSet
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
lhs.add("java");
lhs.add("python");
lhs.add("c++");
lhs.add("java");//duplicate value
lhs.add("html");

//Iterate all the elements from LinkedHashSet
Iterator itr = lhs.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}


output : java
              python
              c++
              html

In the above example we can see it is not containing duplicate values. It maintains insertion order.

Java LinkedHashSet Example 2

This is another example of linked hash set. In this example we will print String values and Integer values. Let's start

import java.util.LinkedHashSet;
class Example2
{
public static void main(String args[])
{
//print String values
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
lhs.add("amit");
lhs.add("karan");
lhs.add("amit");
lhs.add("varun");
lhs.add("arjun");

System.out.println(lhs);

//print Integer values
LinkedHashSet<Integer> lhs1 = new LinkedHashSet<Integer>();
lhs1.add(100);//Integer values
lhs1.add(101);
lhs1.add(102);
lhs1.add(100);
lhs1.add(103);

System.out.println(lhs1);
}
}

output : [amit, karan, varun, arjun]
              [100, 101, 102, 103]



Java LinkedHashSet ForEach Example

In this linked hash set example, we are going  to use for each loop for retrieve all the elements.


import java.util.LinkedHashSet;

class Example2
{
public static void main(String args[])
{
//print String values
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
lhs.add("amit");
lhs.add("karan");
lhs.add("amit");
lhs.add("varun");
lhs.add("arjun");

System.out.println("String Values");

//using for each loop
for(String s : lhs)
{
System.out.println(lhs);
}

//print Integer values
LinkedHashSet<Integer> lhs1 = new LinkedHashSet<Integer>();
lhs1.add(100);//Integer values
lhs1.add(101);
lhs1.add(102);
lhs1.add(100);
lhs1.add(103);

System.out.println("Integer Values");


//using for each loop

for(Integer i : lhs1)
{
System.out.println(lhs1);
}
}

}

output : String Values
              amit
              karan
              varun
              arjun


             Integer Values

             100
             101
             102
             103 


Difference Between HashSet and LinkedHashSet Class

HashSet : Java HashSet class doesn't maintains insertion order of an elements.

LinkedHashSet : Java LinkedHashSet class maintains insertion order of an elements.

Difference Between HashSet and ArrayList Class

HashSet : Java HashSet class does not allow duplicate elements. HashSet does not maintains insertion order.

ArrayList : Java ArrayList class take duplicate elements. ArrayList maintains insertion order of an elements.


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