Tuesday, 16 December 2014

Java : Collection Framework : HashSet (Remove Group of Objects)

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

/*
 * Example of removeAll(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");

        /*
         * Removes from this set all of its elements that are contained in the
         * specified collection.
         */

        boolean isRemoved = hashSet.removeAll(arrayList);

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

    }
}

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

arrayList : [Ram, Dave, Peter]

isRemoved : true
hashSet : [Rohan, Phil]

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

    Google+ Followers

    Google+ Badge