Thursday, November 22, 2018
Home > java > HashSet in java example

HashSet in java example

HashSet in java example

Java HashSet class is used to create a collection that uses a hash table for storage. It implements Set interface and inherits the AbstractSet class.

The important points about Java HashSet class are:

1) HashSet stores the values by using the hashing mechanism.

2) HashSet contains an only unique value.

Difference between List and Set


List can contain duplicate value whereas Set contains only unique values.

Hierarchy of HashSet


HashSet class extends AbstractSet class. AbstractSet class implements Set interface and Set interface inherits Collection and Iterable interfaces.

package of java.util.HashSet class.

Constructors of Java HashSet class


SN

Constructor

Description

1)HashSet()It is used to initialize HashSet.
2)HashSet(int capacity)It is used to initialize the capacity of the HashSet to the specified integer value capacity. The capacity grows automatically as values are added to HashSet.
3)HashSet(int capacity, float loadFactor)It is used to initialize the capacity of the HashSet to the specified integer value capacity and the specified load factor.
4)HashSet(Collection c)It is used to initialize the HashSet by using the values of the Collection c.

Methods of Java HashSet class


SN

Modifier & Type

Method

Description

1)booleanadd(Object o)It is used to adds the given element to this set if it is not already present.
2)voidclear()It is used to remove all values from Set.
3)objectclone()It is used to return a shallow copy of HashSet instance: the values themselves are not cloned.
4)booleancontains(Object o)It return true when Set have the given element.
5)booleanisEmpty()It return true if Set contains no values.
6)Iterator<E>iterator()It return an iterator over the values in Set.
7)booleanremove(Object o)It is used to remove the given values from Set if it is present.
8)intsize()It is used to return the number of values in Set.
9)Spliterator<E>spliterator()It used to create a late-binding and fail-fast Spliterator over the values in Set.

Example of HashSet in java


package demo;

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

public class Demo_HashSet
{
public static void main(String args[])
{
//Creating Object of Hashset Class
HashSet hs=new HashSet();

//Adding Element in HashSet Object
hs.add("London");
hs.add("LA");
hs.add("Newyork");

//Reading Element
Iterator itr=hs.iterator();
while(itr.hasNext())
System.out.println(itr.next());
}
}

OUTPUT

London
LA
Newyork

HashSet Example: Secondary datatype “Student” 


package demo;

import java.util.HashSet;

class Student
{
String name;
int age;
int rollNo;

Student(String name,int age,int rollNo)
{
this.name=name;
this.age=age;
this.rollNo=rollNo;
}
}
public class Demo_HashSet
{
public static void main(String args[])
{
Student s1=new Student("John",12,1);
Student s2=new Student("Peter",14,2);
Student s3=new Student("Smith",15,3);

//Create object of HashSet
HashSet<Student> hs=new HashSet<Student>();

//Adding Object of Student as element
hs.add(s1);
hs.add(s2);
hs.add(s3);

//Retriving data from HashSet
for(Student s:hs)
{
System.out.println(s.rollNo+" "+s.name+" "+s.age);
}
}
}

OUTPUT

3 Smith 15
1 John 12
2 Peter 14
Admin
We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

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