Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java ArrayList retainAll() Method · Private Archive

Java ArrayList retainAll() Method · Private Archive

Java ArrayList Java ArrayList

Java ArrayList retainAll() Method

The retainAll() method is used to retain those elements in the arraylist that also exist in the specified collection, that is, to delete those elements that do not exist in the specified collection.


arraylist.retainAll(Collection c);

**Note:** arraylist is an object of the ArrayList class.

Parameter Description:

Return Value

collection - the collection parameter

Returns true if elements are deleted from the arraylist.

If the elements present in the arraylist class are incompatible with the elements in the specified collection class, a ClassCastException is thrown.

Example

If the arraylist contains a null element and the specified collection does not allow null elements, a NullPointerException is thrown.

Example

import

java.util.ArrayList

;

class

Main

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a dynamic array

        ArrayList

<
String
sites
=
new
ArrayList
<>
(
)

;

       

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Taobao"

)

;

       

System

.

out

.

println

(

"ArrayList 1: "

sites

)

;

       

// Create another dynamic array

        ArrayList

<
String
sites2
=
new
ArrayList
<>
(
)

;

       

// Add elements to the dynamic array

        sites2.

add

(

"Wiki"

)

;

        sites2.

add

(

"Runoob"

)

;

        sites2.

add

(

"Google"

)

;

       

System

.

out

.

println

(

"ArrayList 2: "

sites2

)

;

       

Retaining elements in a specified collection:

        sites.

retainAll

(

sites2

)

;

       

System

.

out

.

println

(

// Retain elements

sites

)

;

   

}

}

The output of the above program is:


ArrayList 1: [Google, Runoob, Taobao]
ArrayList 2: [Wiki, Runoob, Google]
保留的元素: [Google, Runoob]

In the example above, we created two dynamic arrays named sites and sites2.

Note this line:

sites.retainAll(sites2);

"Retained elements: "

In the example, we passed the array sites2 as the parameter of the retainAll() method. This method deletes from sites the elements that do not exist in sites2.

Example

import

java.util.ArrayList

;

import

java.util.HashSet

;

class

Main

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create an array

        ArrayList

<
Integer
numbers
=
new
ArrayList
<>
(
)

;

       

// Add elements to the array

        numbers.

add

(

1

)

;

        numbers.

add

(

2

)

;

        numbers.

add

(

3

)

;

       

System

.

out

.

println

(

"ArrayList: "

numbers

)

;

       

Common elements between ArrayList and HashSet:

        HashSet

<
Integer
primeNumbers
=
new
HashSet
<>
(
)

;

       

// Create a HashSet object

        primeNumbers.

add

(

2

)

;

        primeNumbers.

add

(

3

)

;

        primeNumbers.

add

(

5

)

;

       

System

.

out

.

println

(

"HashSet: "

primeNumbers

)

;

       

// Add elements to the HashSet

        numbers.

retainAll

(

primeNumbers

)

;

       

System

.

out

.

println

(

// Retain common elements in the arraylist

numbers

)

;

   

}

}

The output of the above program is:

ArrayList: [1, 2, 3]
HashSet: [2, 3, 5]
共有元素: [2, 3]

In the example above, we created an ArrayList named numbers and a HashSet named primeNumbers.

Note this line:


numbers.retainAll(primeNumbers);

"Common elements: "

Java ArrayList Java ArrayList