Common Methods for java-15.2 Collection

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.