📜  Java中的AbstractSet类与示例

📅  最后修改于: 2022-05-13 01:55:43.347000             🧑  作者: Mango

Java中的AbstractSet类与示例

Java中的AbstractSet 类是Java集合框架的一部分,它实现了Collection 接口并扩展了AbstractCollection 类。它提供了 Set 接口的框架实现。此类不会覆盖 AbstractCollection 类的任何实现,而只是添加 equals() 和 hashCode() 方法的实现。

通过扩展此类实现集合的过程与通过扩展 AbstractCollection 实现集合的过程相同,只是该类的子类中的所有方法和构造函数都必须遵守 Set 接口施加的附加约束(例如, add 方法不得允许将对象的多个实例添加到集合中)。

Java 中的抽象集类

从类层次图可以得出,它实现了IterableCollection 、 Set 接口。直接子类是 ConcurrentSkipListSet、CopyOnWriteArraySet、EnumSet、HashSet、TreeSet。正如我们上面已经讨论过的,AbstractSet 是一个抽象类,因此应该为其分配一个子类的实例,例如 TreeSet、HashSet 或 EnumSet。

语法:声明

public abstract class AbstractSet
extends AbstractCollection
implements Set

其中E是此 Set 维护的元素类型。

AbstractSet 类的构造函数

1. 受保护的 AbstractSet(): 默认构造函数,但受到保护,它不允许创建 AbstractSet 对象。

AbstractSet as = new TreeSet();

AbstractSet 的方法

METHOD

DESCRIPTION

 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).

示例 1:

Java
// Java Program to Illustrate AbstractSet Class
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args) throws Exception
    {
        // Try block to check for exceptions
        try {
 
            // Creating an empty TreeSet of integer type by
            // creating object of AbstractSet
            AbstractSet abs_set
                = new TreeSet();
 
            // Populating TreeSet
            // using add() method
            abs_set.add(1);
            abs_set.add(2);
            abs_set.add(3);
            abs_set.add(4);
            abs_set.add(5);
 
            // Printing the elements inside above TreeSet
            System.out.println("AbstractSet: " + abs_set);
        }
 
        // Catch block to handle the exceptions
        catch (Exception e) {
 
            // Display exception on console
            System.out.println(e);
        }
    }
}


Java
// Java Program to Illustrate Methods
// of AbstractSet class
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args) throws Exception
    {
 
        // Try block to check for exceptions
        try {
 
            // Creating an empty TreeSet of integer type
            AbstractSet abs_set
                = new TreeSet();
 
            // Populating above TreeSet
            // using add() method
            abs_set.add(1);
            abs_set.add(2);
            abs_set.add(3);
            abs_set.add(4);
            abs_set.add(5);
 
            // Printing the elements inside TreeSet
            System.out.println("AbstractSet before "
                               + "removeAll() operation : "
                               + abs_set);
 
            // Creating an ArrayList of integer type
            Collection arrlist2
                = new ArrayList();
 
            // Adding elements to above ArrayList
            arrlist2.add(1);
            arrlist2.add(2);
            arrlist2.add(3);
 
            // Printing the ArrayList elements
            System.out.println("Collection Elements"
                               + " to be removed : "
                               + arrlist2);
 
            // Removing elements from AbstractSet specified
            // using removeAll() method
            abs_set.removeAll(arrlist2);
 
            // Printing the elements of ArrayList
            System.out.println("AbstractSet after "
                               + "removeAll() operation : "
                               + abs_set);
        }
 
        // Catch block to handle the exceptions
        catch (NullPointerException e) {
 
            // Display exception on console
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出
AbstractSet: [1, 2, 3, 4, 5]

示例 2:

Java

// Java Program to Illustrate Methods
// of AbstractSet class
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args) throws Exception
    {
 
        // Try block to check for exceptions
        try {
 
            // Creating an empty TreeSet of integer type
            AbstractSet abs_set
                = new TreeSet();
 
            // Populating above TreeSet
            // using add() method
            abs_set.add(1);
            abs_set.add(2);
            abs_set.add(3);
            abs_set.add(4);
            abs_set.add(5);
 
            // Printing the elements inside TreeSet
            System.out.println("AbstractSet before "
                               + "removeAll() operation : "
                               + abs_set);
 
            // Creating an ArrayList of integer type
            Collection arrlist2
                = new ArrayList();
 
            // Adding elements to above ArrayList
            arrlist2.add(1);
            arrlist2.add(2);
            arrlist2.add(3);
 
            // Printing the ArrayList elements
            System.out.println("Collection Elements"
                               + " to be removed : "
                               + arrlist2);
 
            // Removing elements from AbstractSet specified
            // using removeAll() method
            abs_set.removeAll(arrlist2);
 
            // Printing the elements of ArrayList
            System.out.println("AbstractSet after "
                               + "removeAll() operation : "
                               + abs_set);
        }
 
        // Catch block to handle the exceptions
        catch (NullPointerException e) {
 
            // Display exception on console
            System.out.println("Exception thrown : " + e);
        }
    }
}

输出:

下面定义了一些其他类或接口的方法,它们以某种方式帮助我们更好地理解 AbstractSet 类,如下所示:

类Java.util.AbstractCollection 中声明的方法

METHOD

DESCRIPTION

add​(E e)Ensures that this collection contains the specified element (optional operation).
 addAll​(Collection c)Adds all of the elements in the specified collection to this collection (optional operation).
clear()Removes all of the elements from this collection (optional operation).
contains​(Object o)Returns true if this collection contains the specified element.
containsAll​(Collection c)Returns true if this collection contains all of the elements in the specified collection.
isEmpty()Returns true if this collection contains no elements.
iterator()Returns an iterator over the elements contained in this collection.
remove​(Object o)Removes a single instance of the specified element from this collection, if it is present (optional operation).
retainAll​(Collection c)Retains only the elements in this collection that are contained in the specified collection (optional operation).
toArray()Returns an array containing all of the elements in this collection.
toArray​(T[] a)Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.
 toString()Returns a string representation of this collection.

接口Java.util.Collection 中声明的方法

METHOD

DESCRIPTION

parallelStream()Returns a possibly parallel Stream with this collection as its source.
removeIf​(Predicate filter)Removes all of the elements of this collection that satisfy the given predicate.
stream()Returns a sequential Stream with this collection as its source.
toArray​(IntFunction generator)Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.

在接口Java.lang.Iterable 中声明的方法

METHOD

DESCRIPTION

forEach​(Consumer action)Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

在接口Java.util.Set 中声明的方法

METHOD

DESCRIPTION

add​(E e)Adds the specified element to this set if it is not already present (optional operation).
 addAll​(Collection c)Adds all of the elements in the specified collection to this set if they’re not already present (optional operation).
 clear()Removes all of the elements from this set (optional operation).
 contains​(Object o)Returns true if this set contains the specified element.
containsAll​(Collection c)Returns true if this set contains all of the elements of the specified collection.
 isEmpty()Returns true if this set contains no elements.
iterator()Returns an iterator over the elements in this set.
remove​(Object o)Removes the specified element from this set if it is present (optional operation).
 retainAll​(Collection c)Retains only the elements in this set that are contained in the specified collection (optional operation).
 size()Returns the number of elements in this set (its cardinality).
spliterator()Creates a Spliterator over the elements in this set.
 toArray()Returns an array containing all of the elements in this set.
 toArray​(T[] a)Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.