Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz


Java HashSet Class in Depth

39 sec read

What are Hash Sets ?

Java HashSet class is used to create a collection that uses a hash table for storage. Sets are used to remove duplicate elements from the list and that's the big advantage of Sets over List.

The important points about Java HashSet class are:


HashSet stores the elements by using a mechanism called hashing.

HashSet contains unique elements only.

Difference between List and Set

List can contain duplicate elements whereas Set contains unique elements only.

Example :-

import java.util.*;

public class JavaHashSetsDemo {
  public static void main(String[] args)
    String[] things = {"Apples","Mango","Strwaberry","Mango"};
    List list = Arrays.asList(things);
    System.out.printf("%s ", list);
    Set set = new HashSet(list);
    System.out.printf("%s ", set);

Output :-

[Apples, Mango, Strwaberry, Mango] 
[Apples, Strwaberry, Mango] 

Recommended Articles

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor