In this chapter we will introduce the common methods of collection.
We take ArrayList as an example below.
Package Com.ray.ch14;import Java.util.arraylist;import Java.util.iterator;public class Test {public static void main ( String[] args) {arraylist<integer> rtnlist = new arraylist<integer> (); Rtnlist.add (1);//Add Method ArrayList <Integer> templist = new arraylist<integer> (); Templist.add (2); Rtnlist.addall (templist);// AddAll Method System.out.println (Rtnlist.contains (1));//contains Method System.out.println (Rtnlist.containsall (templist)) ;//Containsall method for (iterator<integer> Iterator = Templist.iterator (); Iterator.hasnext ();) {//iterator iterator Integer item = (integer) iterator.next (); SYSTEM.OUT.PRINTLN (item);} System.out.println (Rtnlist.size ());//Size Method Rtnlist.retainall (templist);//Retainall Method Rtnlist.add (1);// Add Method Rtnlist.remove (1);//Remove Method Rtnlist.removeall (templist);//RemoveAll Method}}
Output:
True
True
2
2
common operations for Collection
|
Method |
Significance |
|
Boolean Add (E E) |
Add the specified element |
|
Boolean AddAll (collection<? extends e> c) |
Adds all the elements in the specified collection to the new collection |
|
void Clear () |
Remove all elements |
|
Boolean contains (Object o) |
Detects whether the specified element is included, if true, returns false instead |
|
Boolean containsall (collection<?> c) |
Detects if all elements of a collection are included, and if True returns True, the inverse returns false |
|
Boolean equals (Object O) |
Compare the specified object |
|
int Hashcode () |
Return hash code value |
|
Boolean IsEmpty () |
is empty |
|
Iterator<e> Iterator () |
Iterators |
|
Boolean remove (Object o) |
removing objects |
|
Boolean RemoveAll (collection<?> c) |
Remove all elements from a collection |
|
Boolean retainall (collection<?> c) |
Only the intersection of two collection is preserved |
|
Object[] ToArray () |
Returns an array containing all the elements in this collection |
|
<T> T[]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 |
Summary: This chapter mainly introduces the common methods of collection.
This chapter is here, thank you.
-----------------------------------
Directory
The common method of understanding java-15.2 collection from the beginning