Easy Tutorial
For Competitive Exams

Java Programming LinkedHashSet

What is LinkedHashSet?

LinkedHashSet is also an implementation of Set interface, it is similar to the HashSet and TreeSet except the below mentioned differences:

  • HashSet doesn’t maintain any kind of order of its elements.
  • TreeSet sorts the elements in ascending order.
  • LinkedHashSet maintains the insertion order.
  • Elements gets sorted in the same sequence in which they have been added to the Set.

How to perform Basic Operations in LinkedHashSet ?

The Basic Operations are:

  • Create LinkedHashSet object
  • Adding elements to it
  • Getting size of LinkedHashSet object
  • Is the set empty or not

Basic Operations

import java.util.LinkedHashSet;
 
public class Demo {
 
    public static void main(String a[]){
         
        LinkedHashSet s1 = new LinkedHashSet();
        s1.add("first");
        s1.add("second");
        s1.add("third");
        System.out.println(s1);
        System.out.println("LinkedHashSet size: "+s1.size());
        System.out.println("Is LinkedHashSet emplty? : "+s1.isEmpty());
    }
}
Try it Yourself

How to add another collection to LinkedHashSet?

By using addAll() method, we can add all elements from another collection object.

Example

import java.util.HashSet;
import java.util.LinkedHashSet;
 
public class Demo {
 
    public static void main(String a[]){
         
        LinkedHashSet s1 = new LinkedHashSet();
        s1.add("first");
        s1.add("second");
        s1.add("third");
        System.out.println(s1);
        HashSet subSet = new HashSet();
        subSet.add("s1");
        subSet.add("s2");
        s1.addAll(subSet);
        System.out.println("LinkedHashSet content after adding another collection:");
        System.out.println(s1);
    }
}
Try it Yourself

How to delete all elements from LinkedHashSet?

By using clear() method, we can delete all elements at once.

Example

import java.util.LinkedHashSet;
 
public class Demo {
 
    public static void main(String a[]){
         
        LinkedHashSet s1 = new LinkedHashSet();
        s1.add("first");
        s1.add("second");
        s1.add("third");
        System.out.println("My LinkedHashSet content:");
        System.out.println(s1);
        System.out.println("Clearing LinkedHashSet:");
        s1.clear();
        System.out.println("Content After clear:");
        System.out.println(s1);
    }
}
Try it Yourself

How to iterate through LinkedHashSet?

By calling iterator() method you will get Iterator object, through which you can iterate all the elements of the LinkedHashSet.

Example

import java.util.Iterator;
import java.util.LinkedHashSet;
 
public class Demo {
 
    public static void main(String a[]){
        LinkedHashSet s1 = new LinkedHashSet();
        s1.add("first");
        s1.add("second");
        s1.add("third");
        Iterator itr = s1.iterator();
        while(itr.hasNext()){
            System.out.println(itr.next());
        }
    }
}
Try it Yourself

How to copy content of LinkedHashSet to an array?

Example

public class Demo {
 
    public static void main(String a[]){
         
        LinkedHashSet s1 = new LinkedHashSet();
        s1.add("first");
        s1.add("second");
        s1.add("third");
        System.out.println("LinkedHashSet content: ");
        System.out.println(s1);
        String[] strArr = new String[s1.size()];
        s1.toArray(strArr);
        System.out.println("Copied array content:");
        for(String str:strArr){
            System.out.println(str);
        }
    }
}
Try it Yourself

Solve this!!

#20811,20812,20813
Share with Friends