• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Java HashSet Example

July 2, 2014 //  by Krishna Srinivasan//  Leave a Comment

HashSet is a collection of unique elements (objects). The HashSet is available with collection package and extends AbstractSet and implements the set interface. The below diagram shows the HashSet class hierarchy where HashSet is extended from the AbstractSet where AbstractSet implements the set interface and AbstractCollection which is extend from collection framework where collection framework is made up of java interfaces. It does not allow any duplicate elements to be stored.

  • The Hash Set does not guarantee the order of the elements.
  • For storage of collection it uses hash table.
  • HashTable is an array of list used to store the information in a most synchronized way.
  • The advantages of using HashSet is that, it can accept null values and it allows the execution of the basic operation such as add ( ) and remove ( ).

also read:

  • Difference between ArrayList, Vector and LinkedList in Java
  • Annotations in Java 5.0 

Hierarchy Diagram of HashSet

Java HashSet Hierarchy Diagram Example

HashSet Features

  • The does not create any duplicate objects.
  • It accepts null values.
  • Elements are added faster to the HashSet.
  • Execution time is constant for all basic operation like add, remove and contains.

HashSet Syntax

Class HashSet<T>

Here T represents the generic type parameter. We can write a HashSet to store a group of strings, as followed:

HashSet<String> hs=new HashSet<String> ();

HashSet Constructors

  • HashSet( ): This is a default hash set.
  • HashSet(collection c): It contains the objects in the specified collection c.
  • HashSet(int capacity): It is used to specify the initial capacity of hash set and the default capacity of hash set is 16.
  • HashSet (int capacity, float fill ratio): It initialize capacity and fillRatio.

HashSet Methods

Methods Description
boolean add(object key) It is used to add an element object to the HashSet. It returns true when the element is added to HashSet else it returns false.
boolean remove(object key) It is used to remove an element from the HashSet. It returns true if the element is removed else it returns false.
boolean contains(object key) If the value of objects are present in HashSet then it returns true else it returns false.
boolean isEmpty( ) If there is no element present in the HashSet then it returns true else it returns false.
Object clone( ) It is used to create a duplicate copy of the object.
int size( ) It returns the number of elements present in HashSet.
void clear( ) It is used to remove all the elements from the HashSet.

HashSet Example

package myproject;

import java.util.HashSet;
import java.util.Iterator;

public class HashSet1 {
	public static void main(String args[]) {
		HashSet<String> hs = new HashSet<String>();
		hs.add("cricket");
		hs.add("football");
		hs.add("baseball");
		hs.add("cricket");
		System.out.println(hs);

		System.out.println("Elements using iterator:");
		Iterator<String> it = hs.iterator();
		while (it.hasNext())
		{
			System.out.println(it.next());
		}
	}
}
  • In the above example we have used hash set class. We have used the value cricket two times just to see whether it creates any duplicate values in the output or not.
  • HashSet hs = new HashSet(); line creates an HashSet which accepts the string value and creates HashSet object using new operator.
  • hs.add(); line is used to add an elements in the HashSet.
  • System.out.println(“Elements using iterator:”); statement is used for printing the given line in the output which is written inside the bracket.
  • Iterator it = hs.iterator(); statement is uses iterator object to traverse through the elements in the HashSet.
  • it.hasNext() method returns true until it reaches the end of the list.

Java HashSet Example

Category: JavaTag: Java Util

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Java HashTable Example
Next Post: Bootstrap Progress Bars »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact