Java Intermediate

0% completed

Previous
Next
HashSet: Operations

The HashSet class in Java provides various operations to manage collections of unique elements. In this lesson, we will cover several fundamental operations on a HashSet, including inserting, removing, checking size, verifying element existence, and converting to an array. Each section includes an explanation, the syntax used for that operation, and a simple example to illustrate its usage.

1. Inserting Elements

Inserting elements into a HashSet is done using the add() method. This method adds an element if it is not already present in the set. Since HashSet only stores unique elements, duplicate insertions are ignored.

Syntax

boolean add(E e);

Example

Java
Java

. . . .

Example Explanation:

  • Insert Operation:
    • The add("Red"), add("Green"), and add("Blue") calls insert unique color values.
  • Duplicate Handling:
    • Attempting to insert "Red" a second time has no effect.
  • Outcome:
    • The printed HashSet shows only unique colors.

2. Removing Elements

Removing elements from a HashSet can be done by specifying the element to remove. The remove() method deletes the specified element if it exists in the set.

Syntax

boolean remove(Object o);

Example

Java
Java

. . . .

Example Explanation:

  • Removal Operation:
    • The remove("Green") call deletes "Green" from the set.
  • Outcome:
    • The printed HashSet shows the remaining colors without "Green".

3. Updating Elements

HashSet does not support direct updating of elements because it is designed to store unique items. To update an element, you typically remove the old element and add the new one.

Example:

Java
Java

. . . .

Example Explanation:

  • Update Process:
    • "Green" is removed from the set using remove().
    • "Blue" is added using add().
  • Outcome:
    • The set reflects the updated values.

4. Checking the Size

The size() method returns the number of elements currently stored in the HashSet. This is useful for determining how many unique items are present.

Syntax:

int size();

Example:

Java
Java

. . . .

Example Explanation:

  • Size Operation:
    • The size() method counts the unique elements in the set.
  • Outcome:
    • The size is printed, showing the number of elements.

5. Checking for Element Existence

To check whether a HashSet contains a specific element, use the contains() method. This method returns true if the element exists, and false otherwise.

Syntax:

boolean contains(Object o);

Example:

Java
Java

. . . .

Example Explanation :

  • Contains Operation:
    • The contains("Blue") method checks for the presence of "Blue".
  • Outcome:
    • The result is printed, indicating whether "Blue" is in the set.

6. Converting HashSet to an Array

The toArray() method converts the HashSet into an array. This can be useful when you need to process the elements using array-based operations.

Syntax:

Object[] toArray();

Example:

Java
Java

. . . .

Example Explanation:

  • Conversion:
    • The toArray() method converts the HashSet into an array of Objects.
  • Outcome:
    • The array is printed using Arrays.toString(), showing all elements.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next