Tutorials Hut

Java Collections: Set and HashSet 

Set is an interface which extends the collection interface, it represents a real world data structure set. Set is further implemented by Abstract set and then HashClass implements AbstractSet. To actually use and perform operations we always use the implementation of Set one of those implementations is HashSet.

Key points and features related to HashSet/Set:

  • Java HashSet uses hashing algorithms to store values.
  • HashSet always contains unique values, duplicates are simply overridden.
  • It allows null.
  • It is not thread safe, which means it is asynchronous.
  • Ordering is not maintained, it depends on hash value.
  • The default capacity is 16 and the load factor is 0.75.

HashSet Declaration

This is how we can declare java.util.HashSet

Generic

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

Normal

HashSet mySet2 = new HashSet();

Assigned to Set insted HashSet

Set mySet3 = new HashSet<>();

Constructors of HashSet

SN

Constructor

Description

1)

HashSet()

Default HashSet.

2)

HashSet(int capacity)

HashSet with initial capacity.

3)

HashSet(int capacity, float loadFactor)

HashSet with capacity and load factor.

4)

HashSet(Collection<? extends E> c)

HashSet with some collections.

Methods of Java HashSet

SN

Modifier & Type

Method

Description

1)

boolean

add(E e)

Adds an element to set.

2)

void

clear()

Clears elements from the set.

3)

object

clone()

Clonses the set.

4)

boolean

contains(Object o)

Checks if it contains an object in the set.

5)

boolean

isEmpty()

Checks if the set is empty or not.

6)

Iterator<E>

iterator()

Returns iterator.

7)

boolean

remove(Object o)

Removes object from set.

8)

int

size()

Returns the size of the set.

9)

Spliterator<E>

spliterator()

Returns spliterator()

HashSet Example with use of add, remove, addAll, isEmpty, iterator(), removeIf, clear

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

public class CollectionsExamples4 {
public static void main(String[] args) {
    //Generic
    HashSet<String> mySet = new HashSet<String>();
    mySet.add("A");
    mySet.add("B");
    mySet.add("C");
    mySet.add("D");
    mySet.add("D"); //duplicate
     System.out.println("HashSet after adding duplicate: "+ mySet);

    //remove
     mySet.remove("A");

    //isEmpty
    mySet.isEmpty();
    HashSet<String> mySet2 = new HashSet<String>();
     mySet2.add("ZZ");

    //addAll
    mySet.addAll(mySet2);
    //iterator of hashSet class
    Iterator itr = mySet.iterator();
    while(itr.hasNext()) {
        System.out.println(itr.next());
     }

    //remove if
      mySet.removeIf(str->str.contains("BB"));   
    //clear
   mySet.clear()
}
}

Output

HashSet after adding duplicate: [A, B, C, D]
ZZ
B
C
D

Create HashSet From Another List

We may create a hashSet with a list, but remember duplicate elements will be removed.

Example

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class CollectionsExamples4 {
public static void main(String[] args) {
    //Generic
    List myList = new ArrayList<String>();
    myList.add("A");
    myList.add("A");
     myList.add("B");

    HashSet<String> mySet = new HashSet(myList);
    System.out.println(mySet);
}
}

Output

[A, B]

Next Articles To Read

  • LinkedHashSet and TreeSet In Java

 

 


















  • Leave a Reply

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