- LinkedHashSet Class extends extends HashSet degree too Implements Set interface inwards collection interface hierarchy.
- LinkedHashSet implementation differs from HashSet equally it maintains doubly-linked listing running through all of its entries.
- It maintains element's entries inwards laid upward equally per it's insertion order. So it volition permit you lot insertion gild iteration over the set. So when you lot iterate over LinkedHashSet, In which gild it volition supply elements? Answer is As per it's insertion order.
- So you lot volition decease same re-create of elements amongst the gild how they inserted inwards set.
- LinkedHashSetis non synchronized.
Bellow given event volition exhibit you lot basic methods of LinkedHashSet too it's usage.
package JAVAExamples; import java.util.Iterator; import java.util.LinkedHashSet; populace degree LinkedHashSetExample { populace static void main(String[] args) { LinkedHashSet<String> lhs = novel LinkedHashSet<String>(); // Add items to LinkedHashSet lhs.add("one"); lhs.add("two"); lhs.add("three"); lhs.add("four"); // Print LinkedHashSet. System.out.println(lhs); // Get size of LinkedHashSet. System.out.println("LinkedHashSet size Is : " + lhs.size()); // Check if LinkedHashSet is empty? System.out.println("Is LinkedHashSet emplty? : " + lhs.isEmpty()); // Iterate through LinkedHashSet. Iterator<String> itr = lhs.iterator(); System.out.println("LinkedHashSet Items are : "); piece (itr.hasNext()) { System.out.println(itr.next()); } // Remove especial from LinkedHashSet. lhs.remove("three"); // Print LinkedHashSet. System.out.println("Now LinkedHashSet items are : " + lhs); } }
Output :
[one, two, three, four] LinkedHashSet size Is : 4 Is LinkedHashSet emplty? : faux LinkedHashSet Items are : 1 2 3 4 Now LinkedHashSet items are : [one, two, four]