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 often used to store objects a way
What is the difference between an array and a collection class in the same time container?
arrays, although they can also store objects, are immutable in length. The length of the collection is variable. The base data type can be stored in an 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 the collection can store different types of objects.
Why are there so many containers?
Because each container has different ways of storing 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 includes the specified element (optional action).
|
boolean |
addAll(Collection<?
extendsE> 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)
If this collection includes the specified element, it returns true. |
boolean |
containsAll(Collection<?> c)
Assume that this collection includes all elements in the specified collection. Returns True.
|
boolean |
equals(Object o)
This collection is equal to the specified object. |
int |
hashCode()
Returns the hash code value for this collection. |
boolean |
isEmpty()
If this collection does not include an element, it returns true. |
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, assuming it exists (optional action). |
boolean |
removeAll(Collection<?
> c)
Removes all the elements in this collection that are also included in the specified collection (optional action).
|
boolean |
retainAll(Collection<?
> c)
Only those elements in this collection that are also included in the specified collection (optional operation) are retained.
|
int |
size()
Returns the number of elements in this collection.
|
Object[] |
toArray()
Returns an array that includes all the elements in this collection. |
|
toArray(T[] a)
Returns an array that includes all the elements in this collection; the execution-time type of the returned array is the same as the execution-time type of the specified array. |
The parameter type of the Add method is object to facilitate receiving arbitrary types of objects
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. Subclasses using the collection 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/infer whether an element is in a collection System.out.println (A1.contains ("ABC01"));//Infer whether the collection is empty System.out.println (A1.isempty ());} public static void Sop (Object obj) {System.out.prinTLN (obj);}}
=============================== iterator =============================
Iterators are in fact the way that collections are used to take out elements.
The element is removed and the element can be manipulated.
| Method Summary |
boolean |
hasNext()
Suppose there are still elements that can iterate. Returns True.
|
E |
next()
Returns the next element of the iteration.
|
void |
remove()
Removes the last element returned by an iterator from the collection that the iterator points to (optional action). |
public class Main {public static void main (string[] args) {//TODO auto-generated method Stubarraylist al1 = new ArrayList (); Al1.add ("abc01"); Al1.add ("Abc02"); Al1.add ("abc03"); Al1.add ("ABC04");//iterator it = Al1.iterator ();//Get iterators, Used to remove the element//////sop (It.next ()) from the collection;//returns the next element of the iteration. SOP (It.hasnext ());//Suppose there are still elements that can iterate. Returns True//while (It.hasnext ())//{//sop (It.next ()),//}for (Iterator it = Al1.iterator (); It.hasnext ();) {sop (It.next ()) ;}} public static void Sop (Object obj) {System.out.println (obj);}}
Java------collection framework commonality method