Overview
The Java Collection class is primarily derived from two interfaces: Collection and map,collection and map are the root interfaces of the Java Collection framework, which in turn includes sub-interfaces or implementation classes. As a collection of inheritance relationships:
Collection
├list
│├linkedlist
│├arraylist
│└vector
│└stack
└set
Map
├hashtable
├hashmap
└weakhashmap
1.1 Collection Interface
The collection interface is the parent interface of the list, Set, and queue interfaces, and the methods defined in the interface can be used to manipulate both the set set and the list and queue collections.
The collection interface defines the following methods for manipulating the set elements:
BooleanAdd (Object o); This method is used to add an element to the collection. Returns true if the collection element was changed by the add operation. BooleanAddAll (Collection c); This method adds the elements in the set C to the specified collection. Returns True if the collection object has been changed by the operation. voidClear (); Clears all elements in the collection. Changes the collection length to 0.Booleancontains (Object o); Returns whether the specified element is included in the collection. BooleanContainsall (Collection c); Returns whether all elements in the set C are included in the collection. BooleanIsEmpty (); Returns whether the collection is empty. Iterator Iterator (); Returns an iterator object that iterates through the elements in the collection. BooleanRemove (Object o); Deletes the specified element in the collection O. When one or more element o is included in the collection, the elements are deleted and the method returns True. BooleanRemoveAll (Collection c); Removes all elements contained in set C from the collection. If one or more of the elements are deleted, the method returns True. intsize (); The method returns the number of elements in the collection object[] ToArray (); The method transforms the collection into an array, and all the collection elements become the corresponding array elements.
Here's a demonstration:
ImportJava.util.*; Public classcollectiontest{ Public Static voidMain (string[] args) {Collection C=NewArrayList (); //adding elementsC.add ("Monkey King")); //Although the base type value cannot be placed in the collection, Java supports automatic boxingC.add (6); System.out.println ("The number of elements in the C collection is:" +c.size ()); //Delete the specified elementC.remove (6); System.out.println ("The number of elements in the C collection is:" +c.size ()); //determines whether the specified string is includedSystem.out.println ("C" contains \ "Monkey King \" string: "+ C.contains" ("Monkey King"))); C.add ("Lightweight Java EE Enterprise Application Combat"); System.out.println ("Elements of the C collection:" +c); Collection Books=NewHashSet (); Books.add ("Lightweight Java EE Enterprise Application Combat"); Books.add ("Crazy Java Handout"); System.out.println ("Does the C collection fully contain the books collection?" " +C.containsall (books)); //subtract elements from the books collection with the C setC.removeall (books); System.out.println ("Elements of the C collection:" +c); //Delete all elements in the C collectionc.clear (); System.out.println ("Elements of the C collection:" +c); //only elements contained in the C set are left in the books collection .Books.retainall (c); System.out.println ("Elements of the Books collection:" +books); }}
Operation Result:
The number of elements in the C collection is: 2 Thenumber of elementsin the C collection is: 1C whether the collection contains "Monkey King" string:truec elements of the collection: [Monkey king, lightweight Java EE Enterprise Application Combat] Does the C collection fully contain the books collection? falsec elements of the collection: [Monkey King]c Elements of the collection: [Elements of the]books collection: []
Java Foundation--java Collection (i)