This is the 15th day of my participation in Gwen Challenge
The Collection interface
The Collection interface is the parent of the List, Set, Map, and Queue interfaces. Methods defined in the Collection interface apply to both sets and lists and queues.
Some common methods in the Collection interface are as follows:
Boolean add(Object o) Adds an element to the collection, returning true on success, false otherwise. Boolean addAll(Collection C) adds Collection C to the Collection, returns true on success, false otherwise. Void clear() clears all elements in the collection. Boolean contains(Object O) Checks whether the set contains element O, returning true if it does, false otherwise. Boolean containsAll(Collection c) checks whether the Collection containsAll of the elements in Collection c, true if it does, false otherwise. Boolean isEmpty() checks whether the set isEmpty, returns true if it isEmpty, false otherwise. Iterator Iterator () returns an Iterator that iterates through the elements of the collection. Also called iterators. Boolean remove(Object o) Removes the first specified element o from the collection, returning true on success, false otherwise. Boolean removeAll(Object O) Removes all elements o from the set, returns true on success, false otherwise. Boolean retainAll(Colection C) Removes elements not contained in collection C from the collection, returns true if the collection is changed, Otherwise return false int size() returns the number of elements in the collection Object[] toArray() converts the collection to an array.Copy the code
Have you got a big head? Now let’s look at some examples of how to use these methods.
public class Test { public static void main(String[] args) { Collection c = new ArrayList(); // Add element c.dd (1) to the collection; c.add(2); c.add(3); // delete element c.emove (1); // Check if you have the element system.out.println (c.taintains (2)); System.out.println(c.isempty ()); System.out.println(c.size()); system.out.println (c.size()); Collection c2 = new ArrayList(); c2.add(2); c2.add(4); c2.add(7); // check whether set C fully contains set c2 system.out.println (c.containsall (c2)); // Subtract c from c2; C. clear(); [] arr = c2.toarray (); System.out.println(Arrays.toString(arr)); }}Copy the code
Try it yourself!