Friday 10 October 2014

Java : Collection Framework : ArrayList (Retain Objects)























Click here to watch in Youtube : https://www.youtube.com/watch?v=xopIDJtZvMI

ArrayListRetainAllExample.java
import java.util.ArrayList;
import java.util.List;

/*
 * Example of retainAll(Collection<?> c) method
 */
public class ArrayListRetainAllExample
{

    public static void main( String[] args )
    {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("Ram");
        arrayList.add("Dave");
        arrayList.add("Peter");
        arrayList.add("Julia");
        arrayList.add("Akram");

        System.out.println("ArrayList : " + arrayList + "\n");

        List<String> retainList = new ArrayList<String>();
        retainList.add("Dave");
        retainList.add("Peter");

        System.out.println("RetainList : " + retainList + "\n");

        /*
         * Retaining group of elements from the list
         */

        arrayList.retainAll(retainList);

        System.out
                .println("ArrayList after retaining elements which are present in the retainList : "
                        + arrayList + "\n");

    }

}


Output
ArrayList : [Ram, Dave, Peter, Julia, Akram]

RetainList : [Dave, Peter]

ArrayList after retaining elements which are present in the retainList : [Dave, Peter]


To Download ArrayListDemoRetain Project Click the below link

https://sites.google.com/site/javaee4321/java-collections/ArrayListDemoRetain.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment