About HashSet:
If You observe output of HashSet example as below, Output is not in same sequence in which we have inserted elements because HashSet does not maintain insertion order while LinkedHashSet does.
Java HashSet Example :
Output :
null
David
Anuj
Zvika
- HashSet is class which extends AbstractSet and Implements Set Interface
- It does not maintain insertion order
- It allow null
- Contains unique elements Only, same as LinkedHashSet
If You observe output of HashSet example as below, Output is not in same sequence in which we have inserted elements because HashSet does not maintain insertion order while LinkedHashSet does.
Java HashSet Example :
package com.anuj.basic; import java.util.HashSet; import java.util.Iterator; /** * * @author Anuj * Does not maintain insertion order * store unique elements only */ public class HashSetOperations { public static void main(String[] args) { HashSet hashSet = new HashSet(); hashSet.add("Anuj"); hashSet.add("Zvika"); hashSet.add("David"); hashSet.add("David"); hashSet.add(null); Iterator iterator = hashSet.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } } }
Output :
null
David
Anuj
Zvika
No comments:
Post a Comment