Table of contents
1.
Introduction
2.
AbstractSet in Java
3.
Methods of AbstractSet
4.
Methods inherited from class java.util.AbstractCollection
5.
Methods inherited from class java.lang.Object
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

AbstractSet

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The AbstractSet class in Java implements the Collection interface and extends AbstractCollection. It is a part of the Java Collection Framework. To minimize the effort involved in implementing this interface, this class provides a better implementation of the Set interface. Now, let's explore the AbstractSet class in more detail.

Also Read About, Multithreading in java

AbstractSet in Java

In general, extending this AbstractSet class allows one to implement a Set in the same way that someone who implements a Collection by extending AbstractCollection. In contrast, subclasses of this AbstractSet class must implement all methods and constructors specified by the Set interface, such as restricting the number of instances of an object that can be added to a Set.

This class does not override any of the abstractCollection implementations. The package simply adds implementations for equals() and hashCode().

Syntax:

public abstract class AbstractSet<E>
extends AbstractCollection<E>
implements Set<E>

 

Methods of AbstractSet

Methods

Description

equals(Object o)

Checks whether the specified Set and other Set are equal. It returns true if the specified Set is a Set if the two sets are the same 

size and if every member of the specified Set is part of this other Set.

hashCode() This method returns a hash code value for the given Set. It is defined that the sum of the hash codes of elements in a Set is the hash code of the Set as a whole.

 

Here is the java code to show the implementation of the methods mentioned above:

import java.util. * ; //importing classes
public class Main {
  public static void main(String args[]) {
    // creating a Set_1 of String type 
    AbstractSet < String > set_1 = new TreeSet < String > ();

    // adding elements in the set_1 using add()method
    set_1.add("Ninja_1");
    set_1.add("Ninja_2");
    set_1.add("Ninja_3");
    set_1.add("Ninja_3");
    set_1.add("Ninja_4");
    
    //printing the elements of Set_1
    System.out.println("Set 1: " + set_1);
    
    // creating another Set_2 of String type 
    AbstractSet < String > set_2 = new TreeSet < String > ();

    // adding elements in the set_2 using add()method
    set_2.add("Ninja_1");
    set_2.add("Ninja_2");
    set_2.add("Ninja_3");
    set_2.add("Ninja_3");
    set_2.add("Ninja_4");

    // printing the elements of Set_2
    System.out.println("Set 2 :" + set_2);

    // Checking if set_1 is eual to the set_2
    boolean eq = set_1.equals(set_2);

    // printing the boolean value
    System.out.println("Is Set_1 and Set_2 equal? " + eq);
    
    // finding the hashcodes for both sets using the hashCode()
    int hc_1 = set_1.hashCode();
    int hc_2 = set_2.hashCode();
    
    // printing the HashCodes
    System.out.println("HashCode of set_1 is : " + hc_1);
    System.out.println("HashCode of set_2 is : " + hc_2);
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output: 

In order to validate that set_1.equals(set_2) implies that set_1.hashCode() == set_2.hashCode(), both sets set_1 and set_2 must have the same hash code, as required by the Object.hashCode contract.

Try it by yourself on java online compiler.

Methods inherited from class java.util.AbstractCollection

Methods

Description

add  A true value is returned if the call altered the Set. The function returns false if the specified element is already present in the Set and does not allow duplicates.
addAll  All the elements in the specified Set will be added to the Set.
clear It removes each element of the Set by iterating over it. This method will likely be overridden for efficiency.
contains If at least one element is present in the set, it returns true.
containsAll If the set contains all the elements in the specified set, it returns true.
isEmpty If there are no elements in set, it returns true.
iterator This method returns an iterator that iterates over all the elements in the set.
remove An element is located by iterating over the set. It uses the Iterator's remove method to remove the element from the set if it finds it.
removeAll Like an Iterator, it evaluates each element returned by the Set, checking if it is a part of the specified Set. The Iterator's remove method removes it from this Set if it is contained.
retainAll The Iterator iterates over the set, checking each element returned by the Iterator against the specified set to determine whether it is contained within.
size Retrieves the number of elements in the set.
toArray Creates an array containing all elements of the set. 
toString The set is represented in string format by a list of the elements it contains, enclosed in square brackets ("[]").

Here is the java code to show the implementation of some methods mentioned above:

import java.util. * ;
public class Main {
  public static void main(String args[]) {
    AbstractSet < String > set_1 = new TreeSet < String > ();

    //adding elements in the set_1
    set_1.add("Ninja_1");
    set_1.add("Ninja_2");
    set_1.add("Ninja_3");
    set_1.add("Ninja_3");
    set_1.add("Ninja_4");
    System.out.println("Set 1: " + set_1); 

    // Calculating the size 
    System.out.println("Size of Set_1 " + set_1.size()); 

    //Iterating over the set until there is some next value
    Iterator i = set_1.iterator();
    while (i.hasNext()) {
      System.out.println("Value : " + i.next());
    } 

    //Removing a certain element from the set_1
    set_1.remove("Ninja_1");
    System.out.println("Size Set_1 " + set_1); 

    // Clearing the elements from the set_1
    set_1.clear();
    System.out.println("After clearing the Set_1 " + set_1);
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Methods inherited from class java.lang.Object

Methods

Description

clone Creates another object of the same class.
finalize The garbage collector calls this method when the object is no longer referenced.
getClass This method returns an object's runtime class.
notify Activates a single thread that is waiting on this object's monitor.
notifyAll All threads waiting on the monitor of this object will be woken up.
wait  It awaits notification from another thread that this object has changed.

FAQs

  1. What are the exceptions thrown by using the removeAll() method?
    Exceptions are:
    →UnsupportedOperationException: If this set does not support removeAll().
    →ClassCastException: When an element of this set can't be cast to the specified collection.
    →NullPointerException: When this set contains a null element and the collection cannot have null elements.
     
  2. What is the purpose of HashSet extending AbstractSet and implementing Set?
    HashSet implements the Set interface's contract. To avoid having to check the class hierarchy, it's helpful to let the user know that it implements that interface. Extension to AbstractSet is just a matter of implementation. If it stops extending AbstractSet in a future Java version (probably unlikely), it will consistently implement Set.
     
  3. What is the reason AbstractList's hash code computation cannot be the same as AbstractSet's?
    It is quite likely that the hashCode() of two lists with the same elements ordered differently will differ because of the hash code calculation specified in the List interface. Set interfaces guarantee that the hash code of two sets with the same elements in different orders will match.

Key Takeaways

In this blog, we have learned what is AbstractSet in Java and the Methods of AbstractSet inherited from Java.util.AbstractCollection and java.lang.Object.

We hope that this blog has helped you enhance your knowledge about AbstractSet and if you would like to learn more, check out our articles on the link. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass