×

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

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

Sierpinski Number in Java

The Sierpinski triangle—is it a fractal? The Sierpinski Triangle fractals. A self-similar fractal is the Sierpinski triangle. It is made of an equilateral triangle with its residual area successively reduced by...

3 minutes read.

Java Map Generic

Java arrays maintain an ordered collection of things, and the index can be used to access the data (an integer). Unlike HashMap, which stores data as a Key/Value pair. We...

3 minutes read.

Java 9 Tutorial

The Java 9 is most popular release of java programming to make it platform modular. It is used to improve JDK and java implementation security. It provides JAVA SE and...

3 minutes read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

How to Convert String to long in Java

How to Convert String to long in java It is used when you want to perform the mathematical operation on the String which contains long number then the conversion from String...

4 minutes read.

Bounded buffer problem in Java

The Bounded buffer Problem can also be called a Producer consumer problem. The problem covers two processes—the producer and the consumer—that share a single, fixed-size buffer that serves as a...

4 minutes read.

Difference between = = and equals ( ) in java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

6 minutes read.

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Java StringBuffer vs StringBuilder

Java StringBuffer vs StringBuilder StringBuffer The StringBuffer class is used to create mutable string. StringBuffer shows writable and growable character sequences. Java StringBuffer program FileName: StringBufferExample.java // A basic program that demonstrates the working...

2 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

Java Obfuscator

Obfuscation is the process of making something unclear or difficult to interpret. Obfuscators are being used in programming to protect the source code from hackers. In this article, we will...

8 minutes read.

Java program to count the occurrences of each character

The count of each character is the frequency of the characters. For example, the given String is “Programming”. In the given string, the count of the characters ‘P’ -1, ’r’-2,...

6 minutes read.

Nonagonal number in Java

Figureate numbers with the form n(7n-5)/2 are known as nonagonal numbers. 7n+3 will be a triangular number if n is a nonagonal number. The nonagon is included in the idea...

4 minutes read.

Java vs Dot Net

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Bellman Ford Algorithm in Java

Numerous algorithms have been used in dynamic programming to determine the shortest path inside a graph. Among them are Floyd, all-pair shortest path problem, Breadth First Search, Depth First Search,...

6 minutes read.

Java Type Casting

Type casting is a technique or process used in Java to convert one data type into another, either manually or automatically. The compiler performs the automatic conversion, and the programmer...

3 minutes read.