Tuesday, 2 June 2015

How to copy all element of a LinkedHashset to other

import java.util.LinkedHashSet;
import java.util.Set;

public class AddAll {
   
    public static void main(String[] args) {
       
        Set<String> lhs = new LinkedHashSet<String>();
       
        lhs.add("mumbai");
        lhs.add("delhi");
        lhs.add("kolkata");
        lhs.add("chandigarh");
        lhs.add("dehradun");
       
        //print linkedhashset
        System.out.println("lhs is = "+lhs);
       
        LinkedHashSet<String> mylhs = new LinkedHashSet<String>();
       
        /*
         * Adds all of the elements in the specified
         * collection to this collection
         */
        mylhs.addAll(lhs);
       
        //print linkedhashset
               
        for (String str : mylhs) {
            System.out.println("Element is = "+str);
        }
             
    }
   
}


Output:

lhs is = [mumbai, delhi, kolkata, chandigarh, dehradun]

Element is = mumbai

Element is = delhi

Element is = kolkata

Element is = chandigarh

Element is = dehradun

No comments:

Post a Comment