Wednesday 24 December 2014

Java : Collection Framework : LinkedHashSet (RetainAll)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=ChC4t1L75x8&list=UUhwKlOVR041tngjerWxVccw

LinkedHashSetExample.java
import java.util.ArrayList;
import java.util.LinkedHashSet;

/*
 * Example of retainAll(Collection<? extends E> c) method.
 */
public class LinkedHashSetExample
{
    public static void main( String[] args )
    {
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();

        linkedHashSet.add("Rohan");
        linkedHashSet.add("Phil");
        linkedHashSet.add("Ram");
        linkedHashSet.add("Dave");
        linkedHashSet.add("Peter");

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

        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("Ram");
        arrayList.add("Dave");
        arrayList.add("Peter");

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

        /*
         * Retains only the elements in this collection that are contained in
         * the specified collection.
         * 
         * In other words, removes from this collection all of its elements that
         * are not contained in the specified collection..
         */

        boolean isRetained = linkedHashSet.retainAll(arrayList);

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

    }
}

Output
linkedHashSet : [Rohan, Phil, Ram, Dave, Peter]

arrayList : [Ram, Dave, Peter]

isRetained : true
linkedHashSet : [Ram, Dave, Peter]

To Download LinkedHashSetDemoRetainAll Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/LinkedHashSetDemoRetainAll.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