Advertisement



< Prev
Next >



LinkedHashSet Class





LinkedHashSet is a collection class that extends HashSet class. LinkedHashSet stores the element in the order in which they were inserted into LinkedHashSet.




Important features of LinkedHashSet.







Constructors :


1) LinkedHashSet()
This constructor creates an empty LinkedHashSet of a particular object type.
Example -
LinkedHashSet<Integer> arrL= new LinkedHashSet<Integer>();

This constructor example creates an empty LinkedHashSet to hold Integer objects.




2) LinkedHashSet(Collection <? extends E>c)
This constructor creates a LinkedHashSet initialized with the elements of Collection c. With a condition, that, Collection c holds similar type of objects as the type declared by the LinkedHashSet.
LinkedHashSet<Integer> arrL1 = new LinkedHashSet<Integer>():
LinkedHashSet<Integer> arrL2 = new LinkedHashSet<Integer>(arrL1);

This constructor example creates a LinkedHashSet arrL2 initialized with the elements of a LinkedHashSet arrL1. Both LinkedHashSet hold Integer objects.




Some important methods of LinkedHashSet class.


Methods Description
boolean add(E e) Adds a specified element e to the end of an LinkedHashSet.
int size() Returns the total number of elements in an LinkedHashSet.
boolean addAll(Collection<? extends E> c) Adds the content of a Collection c to the end of the HashSet.
void clear() Removes all of the elements from the LinkedHashSet
boolean isEmpty() Returns true is HashSet is empty, else false.
contains(Object o) Checks if LinkedHashSet contains a specific Object.
boolean remove(Object o) Removes the first occurrence of an Object from the LinkedHashSet, if it is present.
Object[] toArray() Returns an Object array containing all the elements of an LinkedHashSet.
Iterator iterator() Returns an Iterator to iterate over elements of an LinkedHashSet.
ListIterator listIterator() Returns a ListIterator to iterate over elements of an LinkedHashSet.



Advertisement




Creating a LinkedHasSet and removing its specific element.


In this program we have created an LinkedHashSet to hold <Integer> objects and we will add/remove some elements from this set
import java.util.*;

class LinkedHashSet1
{
public static void main(String... ar)
{
LinkedHashSet<Integer> linkHash= new LinkedHashSet<Integer>();

linkHash.add(4);
linkHash.add(1);
linkHash.add(5);
linkHash.add(2);
linkHash.add(3);


System.out.println("LinkedHashSet after adding objects = " + linkHash);
System.out.println("Size of LinkedHashSet = "+ linkHash.size());

linkHash.remove(3);
System.out.println("Afer removing element 3, LinkedHashSet = "+ linkHash);

System.out.println("Is this LinkedHashSet empty() : "+ linkHash.isEmpty());

}

}


Output is - :


LinkedHashSet after adding objects = [4, 1, 5, 2, 3]
Size of LinkedHashSet = 5
Afer removing element 3, LinkedHashSet = [4, 1, 5, 2] 
Is this LinkedHashSet empty() : false 


Program Analysis








Creating a new LinkedHasSet from an existing LinkedHashSet


In this program we will create a LinkedHashSet that holds <Integer> objects. We will create and initialize a new LinkedHashSet using the contents of an existing LinkedHashSet.
import java.util.*;

class LinkedHashSet1
{
public static void main(String... ar)
{
LinkedHashSet<Integer> linkHash= new LinkedHashSet<Integer>();

linkHash.add(4);
linkHash.add(1);
linkHash.add(5);
linkHash.add(2);
linkHash.add(3);


System.out.println("LinkedHashSet after adding objects = " + linkHash);
System.out.println("Size of LinkedHashSet = "+ linkHash.size());

System.out.println("Creating a new LinkedHashSet created from an existing LinkedHashSet");

LinkedHashSet<Integer> linkHash2= new LinkedHashSet<Integer>(linkHash);
System.out.println("Contents of new LinkedHashSet : " + linkHash2);
System.out.println("Size of new LinkedHashSet = "+ linkHash2.size());
}

}


Output is - :


LinkedHashSet after adding objects = [4, 1, 5, 2, 3]
Size of LinkedHashSet = 5
Creating a new LinkedHashSet created from an existing LinkedHashSet
Contents of new LinkedHashSet : [4, 1, 5, 2, 3]
Size of new LinkedHashSet = 5


Program Analysis







Method addAll() to add a LinkedHashSet at end of another LinkedHashSet.


import java.util.*;

class LinkedHashSet1
{
public static void main(String... ar)
{
//Creating the first LinkedHashSet 
LinkedHashSet<Integer> linkHash= new LinkedHashSet<Integer>();

linkHash.add(4);
linkHash.add(1);
linkHash.add(5);
linkHash.add(2);
linkHash.add(3);


System.out.println("Contens of first LinkedHashSet= " + linkHash);
System.out.println("Size of first LinkedHashSet = "+ linkHash.size());


//Creating the second LinkedHashSet 
LinkedHashSet<Integer> linkHash2= new LinkedHashSet<Integer>();
linkHash2.add(200);
linkHash2.add(300);
System.out.println("Contents of second LinkedHashSet : " + linkHash2);

//Adding the contents of first LinkedHashSet to the end of second LinkedHashSet
linkHash2.addAll(linkHash);
System.out.println("Contents of second LinkedHashSet after adding first LinkedHashSet to its end : " + linkHash2);
System.out.println("Size of second  LinkedHashSet = "+ linkHash2.size());
}

}


Output is - :


Contens of first LinkedHashSet= [4, 1, 5, 2, 3]
Size of first LinkedHashSet = 5
Contents of second LinkedHashSet : [200, 300]
Contents of second LinkedHashSet after adding first LinkedHashSet to it : [200,300, 4, 1, 5, 2, 3]
Size of second  LinkedHashSet = 7

Method add() is called to add the elements of an existing LinkedHashSet(linkHash) to the end of elements of another LinkedHashSet(linkHash2).



Please share this article -





< Prev
Next >
< HashSet Class
TreeSet Class >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement