Java AbstractCollection与实例

Java AbstractCollection与实例

Java中的 AbstractCollection 类是Java集合框架的一部分,实现了 集合接口。 它被用来实现一个不可修改的集合,为此人们只需要扩展这个AbstractCollection类,并只实现迭代器和大小方法。

类的层次结构

java.lang.Object
 ↳ java.util
    ↳ Class AbstractCollection<E>

语法

public abstract class AbstractCollection<E>
    extends Object
       implements Collection<E>

where **E** is the type of elements maintained
by this collection.

Java中的构造函数 AbstractCollection :

  • protected AbstractCollection() : 默认的构造函数,但由于是受保护的,它不允许创建一个AbstractCollection对象。

下面是说明Java中AbstractCollection的示例程序。

// Java code to illustrate AbstractCollection
 
import java.util.*;
import java.util.AbstractCollection;
 
public class GFG {
    public static void main(String[] args)
    {
        // Create an empty collection
        AbstractCollection<Object>
            abs = new ArrayList<Object>();
 
        // Use add() method to add
        // elements in the collection
        abs.add("Welcome");
        abs.add("To");
        abs.add("Geeks");
        abs.add("4");
        abs.add("Geeks");
 
        // Displaying the Collection
        System.out.println("AbstractCollection: "
                           + abs);
    }
}

输出

AbstractCollection: [Welcome, To, Geeks, 4, Geeks]

Java抽象集合中的方法

  1. add(E e) : 这个方法确保这个集合包含指定的元素(可选操作)。
  2. addAll(Collection c) : 该方法将指定集合中的所有元素添加到这个集合中(可选操作)。
  3. clear() : 这个方法从这个集合中移除所有的元素(可选操作)。
  4. contains(Object o) : 如果这个集合包含指定的元素,该方法返回true。
  5. containsAll(Collection c) : 如果这个集合包含指定集合中的所有元素,该方法返回true。
  6. isEmpty() : 如果这个集合不包含任何元素,该方法返回真。
  7. iterator() : 这个方法返回这个集合中包含的元素的一个迭代器。
  8. remove(Object o) : 这个方法从这个集合中删除一个指定元素的实例,如果它存在的话(可选操作)。
  9. size() : 这个方法返回这个集合中元素的数量。
  10. toArray() : 该方法返回一个包含此集合中所有元素的数组。
  11. toArray(T[] a) : 该方法返回一个包含此集合中所有元素的数组;返回的数组的运行时类型是指定数组的类型。
  12. toString(): 这个方法返回这个集合的字符串表示。

例子

// Java code to illustrate
// methods of AbstractCollection
 
import java.util.*;
import java.util.AbstractCollection;
 
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
 
        // Creating an empty collection
        AbstractCollection<String>
            abs1 = new TreeSet<String>();
 
        // Use add() method to add
        // elements into the Collection
        abs1.add("Welcome");
        abs1.add("To");
        abs1.add("Geeks");
        abs1.add("4");
        abs1.add("Geeks");
        abs1.add("TreeSet");
 
        // Displaying the Collection
        System.out.println("AbstractCollection 1: "
                           + abs1);
 
        // Creating another Collection
        AbstractCollection<String>
            abs2 = new TreeSet<String>();
 
        // Displaying the Collection
        System.out.println("AbstractCollection 2: "
                           + abs2);
 
        // Using addAll() method to Append
        abs2.addAll(abs1);
 
        // Displaying the Collection
        System.out.println("AbstractCollection 2: "
                           + abs2);
 
        // Clearing the collection
        // using clear() method
        abs1.clear();
 
        // Check for the empty collection
        System.out.println("Is the collection empty? "
                           + abs1.isEmpty());
    }
}

输出

AbstractCollection 1: [4, Geeks, To, TreeSet, Welcome]
AbstractCollection 2: []
AbstractCollection 2: [4, Geeks, To, TreeSet, Welcome]
Is the collection empty? true

参考资料 : https://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程