One, the Java collection mainly has the collection and the map collection two interfaces, in Java the collection is to derive by these two interfaces.
The collection interface and the main implementation classes are as follows:
Collection Main API:
Boolean Add (Object obj);
Boolean AddAll (Collection c); Adds the elements of the set C to the specified collection.
void clear (); Clears all elements of the collection, the set length becomes 0
Boolean contains (Object O), whether the collection contains the specified element
Boolean containsall (Collection c); Does the collection contain all the elements in the set C
Boolean isEmpty (); Whether the collection is empty
Iterator Iterator (); Iterate through the elements in the collection
Boolean remove (Object o); Removes the specified element from the collection
Boolean retainall (Collection c); Removes elements from the collection that are not contained in collection C, which is equivalent to changing the collection that invokes the method into the intersection of the collection and the set C
int size (); Returns the number of collection elements
Object[] ToArray (); Convert a collection to an array
code example:
Public classCollectiondemo { Public Static voidMain (string[] args) {//Create ArrayList CollectionCollection List=NewArrayList (); //adding elementsList.add (1); List.add ("Java"); List.add ("C + +"); System.out.println (list.tostring ()+ "Number of elements:" +list.size ()); //Delete ElementList.remove (1); System.out.println (list.tostring ()+ "Number of elements:" +list.size ()); //whether to include elementsSystem.out.println (List.contains ("Java")); //Create HashSet CollectionCollection Set=NewHashSet (); Set.add ("C #"); Set.add (C); Set.add ("Java"); //Determines whether the collection containsSystem.out.println (List.containsall (set)); //gets the intersection of two setsSet.retainall (list); System.out.println (Set.tostring ()); }}
Java Collection Knowledge Summary