Collection Common Functions
Collection is the parent interface of all single-column collections, so some common methods of single-column collections (List and Set) are defined in Collection, which can be used to manipulate all single-column collections. The methods are as follows:
- Public Boolean add (E): Adds a given object to the current collection.
- public void clear(): Clears all elements in the collection.
- Public Boolean remove (E): Delete a given object from the current collection.
- Public Boolean contains (E): Determines whether a given object is included in the current collection.
- public boolean isEmpty(): Determines whether the current collection is empty.
- public int size(): Returns the number of elements in the collection.
- public Object[] toArray(): Stores elements in a collection into an array.
import java.util.ArrayList; import java.util.Collection; public class Collection_basic { public static void main(String[] args) { Collection<String> coll = new ArrayList<>(); System.out.println(coll); // Printed as [], indicating that soString() has been overwritten. /* 1. add Method public boolean add(E e): Add objects to geometry The return value is a Boolean value, which returns true successfully, but it is generally error-free and generally not accepted. */ boolean b1 = coll.add("Zhang San"); // The return value of add is that Boolean value true is add to CD-ROM migrant workers System.out.println("b1:" + b1); System.out.println(coll); // [Zhang three] coll.add("Li Si"); coll.add("Wang Wu"); coll.add("Zhao Liu"); System.out.println(coll); System.out.println("==================="); /* 2. remove Method public boolean remove(E e): Delete elements from collections The return value is boolean, which exists in the collection, deletes successfully, and returns true The collection does not exist, deletion fails, and false is returned. */ boolean b2 = coll.remove("Zhao Liu"); System.out.println("b2:" + b2); boolean b3 = coll.remove("I do not exist."); System.out.println("b3" + b3); System.out.println("==================="); /* 3. contains public boolean contains(E e): Determine whether an element is included in a collection */ boolean b4 = coll.contains("Li Si"); System.out.println("b4:" + b4); System.out.println("==================="); /* 4. isEmpty public boolean isEmpty(); Set returns true for empty, not false for empty */ boolean b5 = coll.isEmpty(); System.out.println("b5:" + b5); System.out.println("==================="); /* 5. size public int size(); Number of elements returned */ int s = coll.size(); System.out.println("size:" + s); System.out.println("==================="); /* 6. toArray public Object[] toArray(); Store elements in a collection in an array */ Object[] arr = coll.toArray(); System.out.println(arr); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } /* 7. clear public void clear(); Empty all elements in the collection, but do not delete the collection, and become an empty collection */ coll.clear(); System.out.println(coll); System.out.println(coll.isEmpty()); } }