Saturday 1 November 2014

Java : Collection Framework : LinkedList (Retain Objects)


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

LinkedListRetainAllExample.java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

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

    public static void main( String[] args )
    {
        LinkedList<Integer> linkedList = new LinkedList<Integer>();
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);
        linkedList.add(4);
        linkedList.add(5);

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

        List<Integer> retainList = new ArrayList<Integer>();
        retainList.add(3);
        retainList.add(4);

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

        /*
         * Retain group of elements in the linkedList
         */

        linkedList.retainAll(retainList);

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

    }

}

Output
linkedList : [1, 2, 3, 4, 5]

RetainList : [3, 4]

linkedList After retaining elements which are present in the retainList : [3, 4]


To Download LinkedListDemoRetainAll Project Click the below link

https://sites.google.com/site/javaee4321/java-collections/LinkedListDemoRetainAll.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