Monday 15 December 2014

Java : Collection Framework : HashSet (Add Object)


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

HashSetExample.java
import java.util.HashSet;

/*
 * Example of add(E e) method.
 */
public class HashSetExample
{
    public static void main( String[] args )
    {
        HashSet<String> hashSet = new HashSet<String>();
        
        System.out.println("hashSet: " + hashSet + "\n");

        /*
         * Adds the specified element to this set if it is not already present.
         * 
         * If this set already contains the element, the call leaves the set
         * unchanged and returns false.
         */

        boolean isAdded = hashSet.add("Dave");

        System.out.println("isAdded : " + isAdded);
        System.out.println("hashSet: " + hashSet + "\n");   
        
        isAdded = hashSet.add("Dave");

        System.out.println("isAdded : " + isAdded);
        System.out.println("hashSet: " + hashSet);  

    }
}

Output
hashSet: []

isAdded : true
hashSet: [Dave]

isAdded : false
hashSet: [Dave]

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