Java Collections add()方法及实例

Java Collections add()方法及实例

java.util.Collection接口add(E element) 用于将元素’element’添加到这个集合中。该方法返回一个布尔值,描述操作的成功与否。如果元素被添加,它就返回true,否则就返回false。

语法

Collection.add(E element)

参数: 该方法接受一个E类型的强制性参数 元素 ,它将被添加到这个集合中。

返回值: 一个 布尔值 ,描述了操作的成功性。如果元素被添加,它返回真,否则返回假。

异常: 这个方法会抛出以下5个异常,如下所示。

  • UnsupportedOperationException: 如果添加操作不被这个集合所支持。
  • ClassCastException: 如果指定元素的类别阻止它被添加到这个集合中。
  • NullPointerException: 如果指定的元素是空的,而这个集合不允许空元素。
  • IllegalArgumentException: 如果该元素的某些属性阻止它被添加到这个集合中。
  • IllegalStateException: 如果由于插入限制,该元素在此时不能被添加。

现在,我们将在不同的类中实现这个方法,因为当涉及到java编程时,它是一个非常重要和必不可少的方法,所以在这里,我们将强调每个类的情况如下。

  • LinkedList class
  • ArrayDeque
  • ArrayList class
  • NullPointerException is Thrown

让我们通过以下干净的java例子来实现上述4种情况下的add()方法。

例子1: LinkedList类

// Java code to illustrate boolean add() method
 
import java.io.*;
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
 
        // creating an empty LinkedList
        Collection<String> list = new LinkedList<String>();
 
        // use add() method to add elements in the list
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
 
        // Output the present list
        System.out.println("The list is: " + list);
 
        // Adding new elements to the end
        list.add("Last");
        list.add("Element");
 
        // printing the new list
        System.out.println("The new List is: " + list);
    }
}

输出

The list is: [Geeks, for, Geeks]
The new List is: [Geeks, for, Geeks, Last, Element]

例2: ArrayDeque类

// Java code to illustrate add() method
 
import java.util.*;
 
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        Collection<String> de_que = new ArrayDeque<String>();
 
        // Use add() method to add elements into the Deque
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
 
        // Displaying the ArrayDeque
        System.out.println("ArrayDeque: " + de_que);
    }
}

输出

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

例3: 使用ArrayList类

// Java code to illustrate add() method
 
import java.io.*;
import java.util.*;
 
public class ArrayListDemo {
    public static void main(String[] args)
    {
 
        // create an empty array list with an initial capacity
        Collection<Integer> arrlist = new ArrayList<Integer>(5);
 
        // use add() method to add elements in the list
        arrlist.add(15);
        arrlist.add(20);
        arrlist.add(25);
 
        // prints all the elements available in list
        for (Integer number : arrlist) {
            System.out.println("Number = " + number);
        }
    }
}

输出

Number = 15
Number = 20
Number = 25

极客们对特殊情况保持警惕,如下面的例子所示,NullPointer 异常将被抛出。

例4 :

// Java code to illustrate boolean add()
// Where NullPointerException is Thrown
 
// Importing required utility classes
import java.util.*;
 
// Main class
// LinkedListDemo
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating an empty ArrayList of string type
        Collection<String> list = new ArrayList<String>();
 
        // Printing and displaying the Arraylist
        System.out.println("The ArrayList is: " + list);
 
        // Note: Here by now we have not added any element/s
 
        // Try block to check for exceptions
        try {
 
            // Appending the null to the list
            // using add() method
            list.add(null);
        }
 
        // Catch block to handle exceptions
        catch (Exception e) {
 
            // Display message when exceptions occurs
            System.out.println("Exception: " + e);
        }
    }
}

输出

The ArrayList is: []

输出解释: 在这里我们需要把它捡起来,因为我们将只接收一个List。因此,记录add()方法是否接受它,是否需要支持null,是很好的做法。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程