Let's learn java programming language with easy steps

Monday, 15 May 2017

Java HashSet

 

Java HashSet Class

HashSet is a class in java collection framework. Java HashSet  class implements Set interface and extends AbstractSet class in java.


Points About HashSet Class in Java

  • HashSet class implements Set interface and extends AbstractSet class.
  • HashSet class doesn't contains duplicate elements.
  • HashSet doesn't maintains the insertion order.
  • HashSet class in non-synchronized.
  • HashSet allow null values.
  • HashSet using hash table for storing the elements in collection.
  • HashSet class implements Serializable and Cloneable interfaces.

Hierarchy of HashSet Class

Java HashSet


In the above hierarchy, HashSet extends AbstractSet class and AbstractSet class implements Set interface.


Constructor of HashSet Class

1) HashSet()

Creates an empty HashSet in Java.

2) HashSet(Collection c)

This constructor is used to initialize the hash set by using the elements of the collection c.

3) HashSet(int capacity)

This constructor is used to initialized the hash set to the given integer value capacity.

4) HashSet(int capacity, float fillRatio)

This constructor is used to initialize both integer and float values. fillRatio also called load capacity).


Methods of Java HashSet Class

There are some useful methods of java hash set class.

1) boolean add(Object o)

This method is used to add the elements to the set.

2) void clear()

This method removes all the elements from the set.

3) boolean contains(Object o)

This method checks the specified elements in the set if specified elements found in the set its return true otherwise false.

4) boolean isEmpty()

If there is no elements in the set , it returns true otherwise return false.

5) Object clone()

This method returns a shallow copy of the HashSet .

6) int size()

This method tells the number of elements in the set.



Difference Between Set and List Interface

Set : Set interface does not contains duplicate elements.

List : List interface contains duplicate elements.


Java HashSet Example

Here are simple program of java hash set class in collection.

import java.util.*;
class HashSetExample
{
public static void main(String args[])
{
//creating HashSet
HashSet<String> hs = new HashSet<String>();
hs.add("red");//adding elements in the set
hs.add("yellow");
hs.add("red");
hs.add("pink");
hs.add("blue");

//Iterate the elements
Iterator i = hs.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}

output : red
              pink
              blue
              yellow

Note : As we know HashSet doesn't contains duplicate elements and doesn't maintains insertion order of elements.


Java HashSet Example 1

This is another example of hash set class with null values. Let's take a look at below example


import java.util.*;

class HashSetExample
{
public static void main(String args[])
{
//creating HashSet
HashSet<String> hs = new HashSet<String>();
hs.add("red");//adding elements in the set
hs.add("yellow");
hs.add("red");
hs.add("pink");
hs.add("blue");


//adding null values

hs.add(null);
hs.add(null);

//printing hash set

System.out.println(hs);



}

}

output : [ red, null, pink, blue, yellow]

Note : In the above example there is no duplicate values in this list including null values.

Q. We can display the duplicate values in HashSet class ?

Ans. No, We can't display the duplicate values in hash set class in java.

Q. HashSet maintains insertion order, yes or not ?

Ans. HashSet does not maintains insertion order of an elements.



Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate