AbstractSet Class in Java with Examples
The AbstractSet class in Java is a part of the Java Collection Framework which implements the Collection interface and extends the AbstractCollection class. It provides a skeletal implementation of the Set interface. This class does not override any of the implementations from the AbstractCollection class, but merely adds implementations for equals() and hashCode() method.
Class Hierarchy:
java.lang.Object
↳ java.util.AbstractCollection<E>
↳ Class AbstractSet<E>
Syntax:
public abstract class AbstractSet<E>
extends AbstractCollection<E>
implements Set<E>
Where E is the type of elements maintained by
this Set.
Constructors in Java AbstractSet:
- protected AbstractSet(): The default constructor, but being protected, it doesn’t allow to create an AbstractSet object.
Below is a sample program to illustrate the java AbstractSet:
// Java code to illustrate AbstractSet import java.util.*; public class GFG1 { public static void main(String[] args) throws Exception { try { // Creating object of AbstractSet<Integer> AbstractSet<Integer> abs_set = new TreeSet<Integer>(); // Populating abs_set abs_set.add(1); abs_set.add(2); abs_set.add(3); abs_set.add(4); abs_set.add(5); // print abs_set System.out.println("AbstractSet: " + abs_set); } catch (Exception e) { System.out.println(e); } } } |
AbstractSet: [1, 2, 3, 4, 5]
Methods in Java AbstractSet:
- equals(Object o): Compares the specified object with this set for equality.
- hashCode(): Returns the hash code value for this set.
- removeAll(Collection c): Removes from this set all of its elements that are contained in the specified collection (optional operation).
Example:
// Java code to illustrate // methods of AbstractSet import java.util.*; public class GFG1 { public static void main(String[] args) throws Exception { try { // Creating object of AbstractSet<Integer> AbstractSet<Integer> abs_set = new TreeSet<Integer>(); // Populating abs_set abs_set.add(1); abs_set.add(2); abs_set.add(3); abs_set.add(4); abs_set.add(5); // print abs_set System.out.println("AbstractSet before " + "removeAll() operation : " + abs_set); // Creating another object of ArrayList<Integer> Collection<Integer> arrlist2 = new ArrayList<Integer>(); arrlist2.add(1); arrlist2.add(2); arrlist2.add(3); // print arrlist2 System.out.println("Collection Elements" + " to be removed : " + arrlist2); // Removing elemnts from AbstractSet // specified in arrlist2 // using removeAll() method abs_set.removeAll(arrlist2); // print arrlist1 System.out.println("AbstractSet after " + "removeAll() operation : " + abs_set); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
AbstractSet before removeAll() operation : [1, 2, 3, 4, 5] Collection Elements to be removed : [1, 2, 3] AbstractSet after removeAll() operation : [4, 5]
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/AbstractSet.html
Recommended Posts:
- AbstractSet removeAll() Method in Java with Examples
- AbstractSet hashCode() Method in Java with Examples
- AbstractSet equals() Method in Java with Examples
- AbstractSet add() method in Java with Example
- AbstractSet contains() method in Java with Example
- AbstractSet containsAll() method in Java with Example
- AbstractSet toString() method in Java with Example
- AbstractSet clear() method in Java with Example
- AbstractSet toArray(T[]) method in Java with Example
- AbstractSet toArray() method in Java with Example
- AbstractSet retainAll() method in Java with Example
- AbstractSet size() method in Java with Example
- AbstractSet isEmpty() method in Java with Example
- Java.util.concurrent.Phaser class in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : SachinShinde1



