×

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks.

In the AbstractSet, the implementation is same as the implementing a collection by extending AbstractCollection. 

The Hierarchy of the AbstractSet class.

AbstractSet Class in Java

The Subclasses of AbstractSet are HashSet, TreeSet, EnumSet, CopyonWriteArraSet. The Syntax of declaring the AbstractSet is

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

Constructor:

  • Protected AbstractSet():
    It is the default constructor of the AbstractSet with the protected access specifier. The protected access specifier doesn’t allow to create a AbstractSet object.

Methods in AbstractSet Class:

  • equals(Object o):
    The equals() is used to comparing the specific object with this set
  • removeall():
    The removeall() is used to remove all the elements from the set.
  • hashcode():
    The hashcode() is used to return the hash code of set.

Methods in AbstractCollection Class:

  • add(E e):
    The add() is used to add the specific element in the collection.
  • addAll(Collection <extends E> c):
    The addAll() adds all the elements from a specified collection.
  • clear():
    The clear() removes all the elements form the collection.
  • containsAll(collection <extends E> c):
    The containsAll() returns true if the collection contains the all the elements of a specified collection.
  • contains(Object o):
    The Conatins() returns true if the collection contains a specific element.
  • isEmpty():
  • The isEmpty() is used to return true if the collection doesn’t any elements.
  • iterator():
    The iterator method is used return iterator from the collection
  • remove(Object o):
    The remove() is used to remove the specific element from the collection.
  • retianAll(Collection<?> c):
    The retainAll() retains the elements from a specific collection.
  • toArray():
    The method is used to return array containing the all elements from the Collection.
  • toString():
    The toString() returns String representation of the Collection.

Methods of Collection interface:

  • parallelStream():
    The parallelStream() returns a parallel stream with the collection.
  • removeIf(Predicate<? Super E> filter):
    The removeIf() removes the elements from the collection if it satisfy the predicate.
  • stream():
    The stream() return the sequential stream of the collection.
  • toArray(intFunction<T[] generator>):
    The toArray() is used to return the array which contains the all the elements of the collection using the generator function.

Methods of Set interface:

  • add(E e):
    The add() method adds the specific element in the set if it is not already present.
  • addAll(Collection <extends E> c):
    The addAll() method add all the elements from a specified set if it is not already present.
  • clear():
    The clear() method removes all the elements form the set.
  • containsAll(collection <extends E> c):
    The containsAll() is used to return true if the collection contains the all the elements of a specified set.
  • contains(Object o):
    The contains() return true if the collection contains a specific element.
  • isEmpty():
    The isEmpty() is used to return true if the collection doesn’t any elements in the Set.
  • iterator():
    The iterator() is used return iterator from the Set.
  • remove(Object o):
    The remove() method removes the specific element from the Set.
  • retianAll(Collection<?> c):
    The retainAll() is used to retain the elements from a specific Set.
  • toArray():
    The toArray() is used to return array containing the all elements from the Set.
  • size():
    The size() method return the number of elements present in the Set.
  • spliterator():
    The spliterator() method create a spliterator over elements in set.

Example:

import java.io.*;
import java.util.*;
public class Main
{
public static void main(String args[])
{
try
{
/*Creating an empty treeset of int datatype using AbstractSet*/
AbstractSet<Integer> a = new TreeSet<Integer>();
/*adding elements into the tree set*/
a.add(10);
a.add(20);
a.add(30);
a.add(40);
a.add(50);
/*Printing the elemnts in the Tree set*/
System.out.println(“AbstractSet elements:”+a);
}
catch (Exception e)
{
System.out.println(e);
}
}
}

Output:

AbstractSet Elements: [10,20,30,40,50]

Example 2:

import java.io.*;
import java.util.*;
public class Main
{
public static void main(String args[])
{
try
{
/*Creating an empty treeset of int datatype using AbstractSet*/
AbstractSet<Integer> a = new TreeSet<Integer>();
/*adding elements into the tree set*/
a.add(10);
a.add(20);
a.add(30);
a.add(40);
a.add(50);
/*Printing the elemnts in the Tree set*/
System.out.println(“AbstractSet elements:”+a);

/*Creating a arraylist of int type*/
Collection<Integer> ar = new ArrayList<Integer>();
ar.add(1);
ar.add(2);
ar.add(3);
/*Printing the elements form the arraylist*/
System.out.println(“Collection Elements:”+ar);
}
catch (Exception e)
{
System.out.println(e);
}
}
}

Related Topics

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.

Generics in Java

Generics in Java Parameterizedtypes mean generic. Generics allow types (Character, Integer, String, …, etc., as well as user-defined types) to act as parameters to interfaces, classes, and methods. Generics in Java...

9 minutes read.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

Java Substring

What is a substring in Java? Here by the name  " substring " itself, we can easily come to know it is a part of a string or a subset of...

4 minutes read.

Java Math tanh() Method

The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value. Syntax: public static double tanh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic tangent is to...

2 minutes read.

Java try catch

Java try catch There can be statements that can cause exceptions, and the exception leads to abnormal termination of the program. To avoid that abnormal termination, those statements that look potent...

4 minutes read.

Objects and Classes in Java

Objects and Classes in Java Classes and objects are the basic concepts of object-oriented programming. It revolves around the real world entity. In Java, the object is a physical and logical...

5 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

What’s New in Java 15

Sealed classes are the new concept that was introduced by Java 15. Sealed classes are a preview feature. Most of the features which are released in java 15 are in...

3 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Spliterator in Java 8

In this tutorial, we will understand the meaning of spliterator in java 8. It is just like any other iterator available in java used to traverse the elements of either...

4 minutes read.

Catalan number in Java

In general mathematics, Catalan numbers can be defined as the sequence of natural numbers that frequently occur in counting problems often encountered in recursively defined objects. Mathematical formula of Catalan number Coming...

3 minutes read.

How to find characters with the maximum number of times in a string java

Problem statement In this problem, users want to find the maximum count of a character from the string and return the character along with its count. Your task is to create...

3 minutes read.

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment...

2 minutes read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

How to resolve Illegal state exceptions in Java

What is IllegalStateException? When a method is called at the incorrect time, a runtime exception called an IllegalStateException is raised in Java. This exception is utilized to show that a method...

3 minutes read.

Shallow copy in Java

Java's most important task is making a copy or clone of an object. In this part, we'll talk about shallow copies in Java and how to make them of Java...

4 minutes read.