Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – LinkedHashSet

Declaration

class LinkedHashSet<E>

LinkedHashSet extends HashSet class.

Duplicates are not allowed.

Elements insertion order is maintained.

It stores elements using hashing + linked list mechanism.

List of LinkedHashSet Constructors (CLICK HERE)

List of LinkedHashSet Methods (CLICK HERE)

Non Generic Demo

In non-generic demo, we have used non-generic LinkedHashSet class, constructor & methods. Therefore it can contain any type of elements (heterogeneous elements) as shown in program below:

Example

 import java.util.LinkedHashSet;

 public class LinkedHashSetNonGen 
 {
     public static void main(String args[])
     {
         LinkedHashSet lhs = new LinkedHashSet();
         lhs.add("Java");  // String added
         lhs.add(10);  // Integer added
         lhs.add('X');  // Character added
         lhs.add(44.66);  // Float added
         System.out.println(lhs);
     }
 }

Output

[Java, 10, X, 44.66]

If we want LinkedHashSet must contain elements of same type then we must use generic LinkedHashSet.

Generic Demo

In the program below we have used generic LinkedHashSet that can contain only String type of elements.
If we tried to insert element other than String, we will get compile time error.

Example – I

 import java.util.LinkedHashSet;

 public class LinkedHashSetGenDemo1 
 {
     public static void main(String args[])
     {
         LinkedHashSet<String> lhs = new LinkedHashSet<String>();
         lhs.add("Java");
         lhs.add("Is");
         lhs.add("Very");
         lhs.add("Simple");
      // lhs.add('Z'); // The method add(String) 
		          // in the type HashSet<String>
		          // is not applicable 
		          // for the arguments (char)
         System.out.println(lhs);
     }
 }

Output

[Java, Is, Very, Simple]

Example – II

 import java.util.LinkedHashSet;

 public class LinkedHashSetGenDemo2 
 {
     public static void main(String args[])
     {
         LinkedHashSet<Integer> lhs = new LinkedHashSet<Integer>();
         lhs.add(100);
         lhs.add(200);
         lhs.add(300);
         lhs.add(400);
         System.out.println(lhs);
         System.out.println("Does HashSet contains 500? :: "+lhs.contains(500));
         System.out.println("Does HashSet contains 200? :: "+lhs.contains(200));
         lhs.remove(300);
         System.out.println("\nAfter removing 300");
         System.out.println(lhs);	
     }
 }

Output

[100, 200, 300, 400]
Does HashSet contains 500? :: false
Does HashSet contains 200? :: true

After removing 300
[100, 200, 400]

Example – III

 import java.util.LinkedHashSet;

 class Employee 
 {
     int emp_id;
     String name;
     double salary;
	
     Employee(int i, String s, double d)
     {
         emp_id = i;
         name = s;
         salary = d;
     }
 }

 public class LinkedHashSetGenDemo3 
 {
     public static void main(String args[])
     {
         Employee e1 = new Employee(222,"DAVID", 65000.98);
         Employee e2 = new Employee(552,"JAMES", 78000.69);
         Employee e3 = new Employee(111,"RICKY", 45987.45);
         Employee e4 = new Employee(999,"JACK", 130006.68);
		
         LinkedHashSet<Employee> lhs = new LinkedHashSet<Employee>();
		
         lhs.add(e1);
         lhs.add(e2);
         lhs.add(e3);
         lhs.add(e4);
		
         for(Employee e:lhs)
         {
             System.out.println(e.emp_id+"  "+e.name+"  "+e.salary);
         }
     }
 }

Output

222 DAVID 65000.98
552 JAMES 78000.69
111 RICKY 45987.45
999 JACK 130006.68

 

Leave a reply