Thursday, 18 December 2014

Java : Collection Framework : HashSet (Check Group of Objects Exists or not)


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

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

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

        /*
         * Returns true if this collection contains all of the elements in the
         * specified collection.
         */

        boolean isExist = hashSet.containsAll(arrayList);

        System.out.println("isExist : " + isExist);

    }
}

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

arrayList : [Ram, Dave, Peter]

isExist : true

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