Java Online Compiler
Linked HashSet
Linked HashSet is similar to hashset class.It is used to handle the data very efficiently by not allowing duplication.
Difference Between HashSet and LinkedHashSet ?
  • HashSet store the elements in the ramdom memory location. Whereas LinkedHashSet stores the elements in the sequencial memory location.
LinkedHshSet also supports same methods which are supported by the HashSet classes.
Linked HashSet ADD Method:
It is used to add the element to Set.

import java.util.*;
class LinkedHashSetAdd
{
void list()
{
LinkedHashSet<Integer> al=new LinkedHashSet<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
al.add(50);
System.out.println("The List Items Are "+al);
}
}

class Main
{
public static void main(String aargs[])
{
LinkedHashSetAdd d=new LinkedHashSetAdd();
d.list();
}
}

LinkedHashSet Clear Method:
It is used to remove all the elemnts.

import java.util.*;
class LinkedHashSetClear

{
void list()
{
LinkedHashSet<Integer> al=new LinkedHashSet<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
al.add(50);
System.out.println("The List Items Are "+al);
al.clear();
System.out.println("The List Items Are "+al);



}
}

class Main
{
public static void main(String aargs[])
{
LinkedHashSetClear d=new LinkedHashSetClear();
d.list();
}
}

LinkedHashSet Clone Method
It is used to generate duplicate memory space.

import java.util.*;
class LinkedHashSetClone

{
void list()
{
LinkedHashSet<Integer> al=new LinkedHashSet<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
al.add(50);
System.out.println("The List Items Are "+al);
LinkedHashSet<Integer> al2;
al2=(LinkedHashSet) al.clone();
System.out.println("After Cloning Elements Are "+al2);
}
}

class Main
{
public static void main(String aargs[])
{
LinkedHashSetClone d=new LinkedHashSetClone();
d.list();
}
}

LinkedHashSet Remove Method
It is used to remove the specified element

import java.util.*;
class LinkedHashSetRemove

{
void list()
{
LinkedHashSet<Integer> al=new LinkedHashSet<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
al.add(50);
System.out.println("The List Items Are "+al);
al.remove(50);
System.out.println("The List Items Are "+al);

}
}

class Main
{
public static void main(String aargs[])
{
LinkedHashSetRemove d=new LinkedHashSetRemove();
d.list();
}
}

LinkedHashSet Size Method
It returns the number of elements of the Set.

import java.util.*;
class LinkedHashSetSize

{
void list()
{
LinkedHashSet<Integer> al=new LinkedHashSet<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
al.add(50);
System.out.println("The List Items Are "+al);

System.out.println(al.size());
}
}

class Main
{
public static void main(String aargs[])
{
LinkedHashSetSize d=new LinkedHashSetSize();
d.list();
}
}