Advertisement
1) LinkedHashSet() |
---|
This constructor creates an empty LinkedHashSet of a particular object type. Example - |
LinkedHashSet<Integer> arrL= new LinkedHashSet<Integer>();
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);
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 |
Returns an Iterator to iterate over elements of an LinkedHashSet. |
ListIterator |
Returns a ListIterator to iterate over elements of an LinkedHashSet. |
Advertisement
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());
}
}
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
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());
}
}
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
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());
}
}
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement