JavaBeat

  • Home
  • 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)
  • Privacy

HashSet in Java

January 21, 2014 by Krishna Srinivasan Leave a Comment

HastSet is one of the classes available with collections package and it extends the AbstractSet and implements the Set interface. This uses the hash table for storing the values. Hash table internally using hashing mechanism to store the values. In the hashing technique, information is uniquely identified using the hash code. HashSet supports four constructors. This allows the null values.

  • Default Constructor – HashSet()
  • One with initial set of collections as parameter – HashSet(Collection c)
  • Creating HashSet with initial capacity – HashSet(int capacity)
  • Initial capacity and the fill ration to grow the capacity after certain elements inserted – HashSet(int capacity, float fillRatio)

HashSet Methods

The following are the list of methods defined in the HashSet class. This apart from the inherited methods from the parent classes.

  • boolean add(Object obj) –> Adds the element if it is not aleady there
  • void clear() –> Clears all the element from the set
  • Object clone() –> Creates a shallow copy of this HashSet instance
  • boolean contains(Object obj) –> Checks and returns true if this set contains the specified element
  • boolean isEmpty() –> Returns true if this set is empty
  • Iterator iterator() –> Returns an iterator over the elements in this set
  • boolean remove(Object obj) –> Removes the specified element from this set if it is present
  • int size() –> Returns the number of elements in this set (its cardinality)

HashSet Example

[code lang=”java”]
package javabeat.net.core;

import java.util.HashSet;

public class HashSetDemo {
public static void main(String args[]) {
// create a hash set with default constructor
HashSet<String> hashSet = new HashSet<String>();
// add elements to the hash set
hashSet.add("Element 1");
hashSet.add("Element 2");
hashSet.add("Element 3");
System.out.println(hashSet);
}
}

[/code]

Output of the above example will be:

[code]
[Element 3, Element 2, Element 1]
[/code]

Filed Under: Java Tagged With: Java Basics, Java Collections

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.

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.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved