Collection Class
Why does the collection class appear
Object-oriented language is the embodiment of things in the form of objects, so in order to facilitate the operation of more than one object, the object is stored, the collection is the most common way to store objects
What is the difference between an array and a collection class in the same container?
Arrays can also store objects, but the length is immutable, the length of the collection is variable, the base data type can be stored in the array, and the collection can only store objects.
Features of the collection class:
Collections are used only to store objects, the collection length is mutable, and collections can store different types of objects.
Why are there so many containers?
Because each container has a different way of storing the data
This storage method is called: Data structure
Common methods: (Collection method)
increase, delete, change, check.
|
Method Summary |
|
Boolean |
Add (e E)
Make sure that this collection contains the specified element (optional action). |
|
Boolean |
AddAll (collection<? extends e> c)
Adds all the elements in the specified collection to this collection (optional action). |
|
void |
Clear ()
Removes all the elements in this collection (optional action). |
|
Boolean |
Contains (Object o)
Returns True if this collection contains the specified element. |
|
Boolean |
Containsall (collection<?> c)
Returns True if this collection contains all the elements in the specified collection. |
|
Boolean |
Equals (Object o)
Compares this collection with the specified object for equality. |
|
Int |
Hashcode ()
Returns the hash code value for this collection. |
|
Boolean |
IsEmpty ()
Returns True if this collection contains no elements. |
|
Iterator<e> |
Iterator ()
Returns an iterator that iterates over the elements of this collection. |
|
Boolean |
Remove (Object o)
Removes a single instance of the specified element from this collection, if one exists (optional action). |
|
Boolean |
RemoveAll (collection<?> c)
Removes all the elements in this collection that are also contained in the specified collection (optional action). |
|
Boolean |
Retainall (collection<?> c)
Leave only those elements in this collection that are also contained in the specified collection (optional action). |
|
Int |
Size ()
Returns the number of elements in this collection. |
|
Object[] |
ToArray ()
Returns an array that contains all the elements in this collection. |
|
|
ToArray (t[] a)
Returns an array containing all the elements in this collection; The run-time type of the returned array is the same as the run-time type of the specified array. |
The argument type of the Add method is object to facilitate receiving any type of object
A reference to an object is stored in the collection (address)
Import Java.util.arraylist;public class Main {public static void Main (string[] args) {//method_1 (); Method_2 ();} public static void Method_2 () {ArrayList al1 = new ArrayList (); ArrayList Al2 = new ArrayList (), Al1.add ("Abc01"), Al1.add ("Abc02"), Al1.add ("abc03"), Al1.add ("ABC04"); Al2.add ("Abc01 Al2.add ("Abc02"), Al2.add ("ABC05"), Al2.add ("abc06"), Al1.removeall (AL2);//Remove the intersection of AL1 and Al2, save in Al1 sop (AL1); SOP ( AL2);/* * Print results: * [ABC03, ABC04] * * [ABC01, ABC02, ABC05, ABC06] * */}public static void Method_1 () {//Create a collection container using CO Subclass of the Llection interface arraylistarraylist a1 = new ArrayList (),//Add Element A1.add ("ABC01"), A1.add ("Abc02"), A1.add ("abc03"); A1.add ( "ABC04");//Get the number set length System.out.println (A1.size ());//Print Set SYSTEM.OUT.PRINTLN (A1);//delete element A1.remove ("abc03"); SYSTEM.OUT.PRINTLN (A1);/* * Print results 4 [ABC01, ABC02, ABC03, ABC04] [Abc01, ABC02, ABC04] *///a1.clear ();//Empty set//Determine whether an element is in a collection System.out.println (A1.contains ("ABC01"));//Determines whether the set is empty System.out.println (A1.isempty ());} public static void Sop (Object obj) {System.out.prinTLN (obj);}}
Java------collection framework commonality method