HashSet Example Java Program
Definition
A set can't have any duplicate elements in it. Additionally, the set has no set order. As such, elements can't be found by index. HashSet uses a hash table.Syntax
HashSetVariable_name = new HashSet();
HashSet Example Program
import java.util.*; class HashSetExample{ public static void main(String[] args){ HashSeths=new HashSet (); hs.add("d"); hs.add("c"); hs.add("b"); hs.add("a"); hs.add("c"); System.out.println("Hashset is "+hs); System.out.println("Size of Hashset is "+hs.size()); } }
Sample Output
Output is: Hashset is [d, b, c, a] Size of Hashset is 4
Java Collection Programs
- Linked List Example Java Program
- ArrayList Example Java Program
- HashSet Example Java Program
- Clear an Arraylist Example Java Program
- Reversing an ArrayList Example Java Program
- Linked HashSet Example Java Program
- HashMap Example Java Program
- Set Value in ArrayList Example Java Program
- Linked HashMap Example Java Program
- Remove Element from ArrayList Example Java Program
- Identity HashMap Example Java Program
- TreeMap Example Java Program
- TreeSet Example Java Program
- Finding Duplicates in Array Using TreeSet Java Example Program
- HashTable Example Java Program
- EnumSet Example Java Program
- Enum Map Example Java Program
- Wrapper Example Java Program
- Un Modifiable Wrapper Example Java Program
- Iterator Interface Example Java Program
- Sorted Map Interface Example Java Program
- Sorted Set Interface Example Java Program
Read More Articles
- Multiple Inheritance Using Interface Example Java Program
- Single Inheritance Example Java Program
- Multilevel Inheritance Example Java Program
- Hierarchical Inheritance Example Java Program
- Find all Substrings of a given string Example Java Program
- Create Matrix Example Java Program
- Sum Of Three Numbers Example Java Program
- Heap Sort Example Java Program
- Twin Prime Example Java Program
- Compile Time Polymorphism Example Java Program