Thursday 18 December 2014

Java : Collection Framework : HashSet (RetainAll)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=9T-b5jgMZ2M&list=UUhwKlOVR041tngjerWxVccw

HashSetExample.java
import java.util.ArrayList;
import java.util.HashSet;

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

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

        System.out.println("hashSet : " + hashSet + "\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 = hashSet.retainAll(arrayList);

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

    }
}

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

arrayList : [Ram, Dave, Peter]

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

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