HashSet Java Class

Article by: Manish Methani

Last Updated: October 23, 2021 at 10:04am IST
39 sec read

What are Hash Sets?

HashSet java 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 the HashSet Java class are:

HashSet stores the elements by using a mechanism called hashing.

HashSet contains unique elements only.

Difference between List and Set

A basic difference between a List and a Set in Java is that list in Java can contain duplicate elements whereas a Set in Java 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);
    System.out.println();
    
    Set set = new HashSet(list);
    System.out.printf("%s ", set);
  }
  
}

Output:-

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

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>