Export (0) Print
Expand All

List.retainAll Method

Visual Studio 2005

Deletes the elements of the current List collection that do not exist in specified collection object.

Package: java.util

Assembly: vjslib (in vjslib.dll)


public abstract boolean retainAll(
    java.util.Collection c);

Parameters

c

The specified collection.

true if the list has changed after the call; false otherwise.

In this example, you create and initialize two collections, aList and lList. Then you delete the elements of lList that do not exist in aList and display both collections before and after.

// List-retA1.jsl
// List.retainAll example

import java.util.*;

public class MyClass
{
    public static void main(String[] args)
    {
        // Create two collections:
        LinkedList lList =  new LinkedList();
        ArrayList aList =  new ArrayList();

        // Add some elements to lList:
        lList.add("Isabella");
        lList.add("Angelina");
        lList.add("Pille");
        lList.add("Hazem");

        // Add some elements to aList:
        aList.add("Isabella");
        aList.add("Angelina");
        aList.add("Bianca");

        // Display the two collections before:
        System.out.println("The aList collection before: " + aList);
        System.out.println("The lList collection before: " + lList);

        // Delete the elements lList that are not in aList:
        lList.retainAll(aList);

        // Display the collection after:
        // Display the two collections before retaining all:
        System.out.println("The aList collection after: " + aList);
        System.out.println("The lList collection after: " + lList);
    }
}

/*
Output:
The aList collection before: [Isabella, Angelina, Bianca]
The lList collection before: [Isabella, Angelina, Pille, Hazem]
The aList collection after: [Isabella, Angelina, Bianca]
The lList collection after: [Isabella, Angelina]
*/

Community Additions

ADD
Show:
© 2014 Microsoft