• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Linked HashSet




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 random memory location. Whereas LinkedHashSet stores the elements in the sequential memory location. LinkedHshSet also supports the 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 al=new LinkedHashSet();
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 args[])
{
LinkedHashSetAdd d=new LinkedHashSetAdd();
d.list();
}
}

LinkedHashSet Clear Method:
It is used to remove all the elements.
import java.util.*;
class LinkedHashSetClear
{
void list()
{
LinkedHashSet al=new LinkedHashSet();
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 args[])
{
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 al=new LinkedHashSet();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
al.add(50);
System.out.println("The List Items Are "+al);
LinkedHashSet al2;
al2=(LinkedHashSet) al.clone();
System.out.println("After Cloning Elements Are "+al2);
}
}

class Main
{
public static void main(String args[])
{
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 al=new LinkedHashSet();
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 args[])
{
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 al=new LinkedHashSet();
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 args[])
{
LinkedHashSetSize d=new LinkedHashSetSize();
d.list();
}
}