Wednesday 27 May 2015

How to copy all element of a Hashset to other

import java.util.HashSet;
import java.util.Set;

public class AddAll {

    public static void main(String[] args) {

        Set<String> hs = new HashSet<String>();
        hs.add("java");
        hs.add("php");
        hs.add("html");
        hs.add("javascript");
        hs.add("mysql");

        // print hashset
        System.out.println("hs is = " + hs);
       
        HashSet<String> hashSet = new HashSet<String>();
       
        /*
         * Adds all of the elements in the specified
         *  collection to hashset collection
         */
       
        hashSet.addAll(hs);
       
        System.out.println("hashset is="+hashSet);

    }

}





Output:


hs is = [javascript, php, html, java, mysql]

hashset is=[javascript, php, java, html, mysql]

No comments:

Post a Comment