Common Methods for java-15.2 Collection
This section describes common Collection methods.
The following uses ArrayList as an example.
Package com. ray. ch14; import java. util. ArrayList; import java. util. Iterator; public class Test {public static void main (String [] args) {ArrayList
RtnList = new ArrayList
(); RtnList. add (1); // add method ArrayList
TempList = new ArrayList
(); TempList. add (2); rtnList. addAll (tempList); // addAll method System. out. println (rtnList. contains (1); // The contains method System. out. println (rtnList. containsAll (tempList); // containsAll Method for (Iterator
Iterator = tempList. iterator (); iterator. hasNext ();) {// Iterator iterator Integer item = (Integer) Iterator. next (); System. out. println (item);} System. out. println (rtnList. size (); // The 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 Collection operations
Method |
Meaning |
Boolean add (E e) |
Add specified Element |
Boolean addAll (Collection c) |
Add all elements in the specified collection to the new collection. |
Void clear () |
Remove all elements |
Boolean contains (Object o) |
Checks whether the specified element is included. If true is returned, false is returned. |
Boolean containsAll (Collection c) |
Checks whether all elements of a Collection are contained. If true is returned, false is returned. |
Boolean equals (Object o) |
Compare the specified object |
Int hashCode () |
Returns the hash value. |
Boolean isEmpty () |
Empty or not |
Iterator Iterator () |
Iterator |
Boolean remove (Object o) |
Remove objects |
Boolean removeAll (Collection c) |
Removes all elements of a Collection. |
Boolean retainAll (Collection c) |
Only the intersection of two collections is retained. |
Object [] toArray () |
Returns an array containing all elements in the collection. |
T [] toArray (T []) |
Returns an array containing all elements in the collection. returns the same runtime type as the runtime type of the specified array. |
Summary: This section describes common Collection methods.
This chapter is here. Thank you.