public
LinkedHashSet()
Constructs
a new, empty linked hash set with the default initial capacity (16)
and load factor (0.75).
import java.util.*; class LinkedHashSetConstructor1{ public static void main(String args[]){ LinkedHashSet<Integer> lnkHashSet; HashSet<Integer> hashSet; lnkHashSet = new LinkedHashSet<> (); hashSet = new HashSet<> (); /* Add Elements to lnkHashSet */ lnkHashSet.add(10); lnkHashSet.add(50); lnkHashSet.add(-33); lnkHashSet.add(987); lnkHashSet.add(1); lnkHashSet.add(-76); /* Add Elements to hashSet */ hashSet.add(10); hashSet.add(50); hashSet.add(-33); hashSet.add(987); hashSet.add(1); hashSet.add(-76); System.out.println("Elements in lnkHashSet"); System.out.println(lnkHashSet); System.out.println("Elements in hashSet"); System.out.println(hashSet); } }
Output
Elements in lnkHashSet [10, 50, -33, 987, 1, -76] Elements in hashSet [-76, 1, 987, 50, 10, -33]
As You observe the output, Elements in LinkedHashSet are in the order they inserted, where as HashSet is not.
No comments:
Post a Comment