Movatterモバイル変換


[0]ホーム

URL:


Open In App

In Java, theretainAll()method is used to retain only the elements in a collection that are also present in another collection. It modifies the current collection by removing elements that are not in the specified collection.

Example 1: This example demonstrateshow the retainAll() method retains only the common elements between two sets.

Java
// Java Program to demonstrate// the working of retainAll() with Setsimportjava.util.*;publicclassGeeks{publicstaticvoidmain(String[]args){// Create two sets of integersSet<Integer>s1=newHashSet<>(Arrays.asList(1,2,3,4,5));Set<Integer>s2=newHashSet<>(Arrays.asList(3,4,5,6,7));System.out.println("Set 1: "+s1);System.out.println("Set 2: "+s2);// Retain only the elements in// s1 that are also in s2s1.retainAll(s2);System.out.println("Modified Set 1 after retainAll: "+s1);}}

Output
Set 1: [1, 2, 3, 4, 5]Set 2: [3, 4, 5, 6, 7]Modified Set 1 after retainAll: [3, 4, 5]

Syntax of retianAll() Method

boolean retainAll(Collection<?> c)

  • Parameter: This method takes collection as a parameter
  • Return Type: This method returns "true" if the current collection was modified by retaining common elements otherwise return "false".

Example 2: This example demonstrates how the retainAll() methodmodifies a set by retaining only the common elements with another set, and shows the return value indicating whether the set was modified.

Java
// Java program to demonstrate how retainAll()// method returns boolean value with Setsimportjava.util.*;publicclassGeeks{publicstaticvoidmain(String[]args){// Create two sets of integersSet<Integer>s1=newHashSet<>(Arrays.asList(1,2,3,4,5));Set<Integer>s2=newHashSet<>(Arrays.asList(3,4,5,6,7));System.out.println("Set 1: "+s1);System.out.println("Set 2: "+s2);// Call retainAll and check the return valuebooleanb=s1.retainAll(s2);System.out.println("Set 1 after retainAll (common elements): "+s1);System.out.println("Was the set modified? "+b);// Modify s1 to contain all elements from s2s1=newHashSet<>(Arrays.asList(3,4,5));// Now retain elements common between s1 and s2b=s1.retainAll(s2);System.out.println("Set 1 after retainAll (common elements): "+s1);System.out.println("Was the set modified? "+b);}}

Output:

Output


Example 3: This example demonstrates thatcalling retainAll() with a null collection as a parameter throws aNullPointerException.

Java
// Java Program to demonstrates retainAll()// method throws NullPointerExceptionimportjava.util.*;publicclassGeeks{publicstaticvoidmain(String[]args){// Create a set with null valuesSet<Integer>s1=newHashSet<>(Arrays.asList(1,2,3,null));Set<Integer>s2=null;s1.retainAll(s2);}}

Output:

output

Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp