The common method of understanding java-15.2 collection from the beginning

Source: Internet
Author: User

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

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.