AbstractSet add() method in Java with Example Last Updated : 24 Dec, 2018 Comments Improve Suggest changes Like Article Like Report The add(E) method of AbstractSet Class appends the specified element to the end of this AbstractSet. Syntax: boolean add(E element) Parameters: This function accepts a single parameter element as shown in the above syntax. The element specified by this parameter is appended to end of the AbstractSet. Return Value: This method returns True after successful execution, else False. Exception: This method throws: UnsupportedOperationException: if the add operation is not supported by this collection ClassCastException: if the class of the specified element prevents it from being added to this collection NullPointerException: if the specified element is null and this collection does not permit null elements IllegalArgumentException: if some property of the element prevents it from being added to this collection IllegalStateException: if the element cannot be added at this time due to insertion restrictions Below program illustrates the working of java.util.AbstractSet.add(E element) method: Example 1: Java // Java code to illustrate boolean add(E element) import java.util.*; public class AbstractSetDemo { public static void main(String args[]) { // Creating an empty AbstractSet AbstractSet<String> set = new TreeSet<String>(); // Use add() method // to add elements in the AbstractSet set.add("Geeks"); set.add("for"); set.add("Geeks"); set.add("10"); set.add("20"); // Output the present AbstractSet System.out.println("The AbstractSet is: " + set); // Adding new elements to the end set.add("Last"); set.add("Element"); // Printing the new AbstractSet System.out.println("The new AbstractSet is: " + set); } } Output: The AbstractSet is: [10, 20, Geeks, for] The new AbstractSet is: [10, 20, Element, Geeks, Last, for] Example 2: To demonstrate NullPointerException Java // Java code to illustrate // boolean add(E element) import java.util.*; public class AbstractSetDemo { public static void main(String args[]) { // Creating an empty AbstractSet AbstractSet<Integer> set = new TreeSet<Integer>(); // Use add() method // to add elements in the AbstractSet set.add(10); set.add(20); set.add(30); set.add(40); set.add(50); // Output the present AbstractSet System.out.println("The AbstractSet is: " + set); System.out.println("Trying to add null"); try { // Adding null set.add(null); } catch (Exception e) { System.out.println(e); } } } Output: The AbstractSet is: [10, 20, 30, 40, 50] Trying to add null java.lang.NullPointerException Comment More infoAdvertise with us Next Article AbstractList addAll() method in Java with Examples C code_r Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-AbstractSet +1 More Practice Tags : JavaJava-Collections Similar Reads AbstractList addAll() method in Java with Examples The addAll() method of java.util.AbstractList class is used to insert all of the elements in the specified collection into this list at the specified position. This shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elem 5 min read AbstractList addAll() method in Java with Examples The addAll() method of java.util.AbstractList class is used to insert all of the elements in the specified collection into this list at the specified position. This shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elem 5 min read AbstractList addAll() method in Java with Examples The addAll() method of java.util.AbstractList class is used to insert all of the elements in the specified collection into this list at the specified position. This shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elem 5 min read AbstractQueue add() method in Java with examples The add(E e) method of AbstractQueue inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. It returns true upon success and throws an IllegalStateException if no space is currently available. Syntax: public boolean add(E e) Para 2 min read AbstractQueue add() method in Java with examples The add(E e) method of AbstractQueue inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. It returns true upon success and throws an IllegalStateException if no space is currently available. Syntax: public boolean add(E e) Para 2 min read AbstractQueue add() method in Java with examples The add(E e) method of AbstractQueue inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. It returns true upon success and throws an IllegalStateException if no space is currently available. Syntax: public boolean add(E e) Para 2 min read Like