Tuesday, 2 June 2015

How to Remove Set From LinkedHashSet


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

public class RemoveAll {

    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(" Before remove element lhs is = "+lhs);
       
        Set<String> set = new HashSet<String>();
        set.add("java");
        set.add("php");
        set.add("mumbai");
        set.add("dehradun");
       
        /*
         * Removes from this set all of its elements
         *  that are contained in the specified collection
         */
       
        lhs.removeAll(set);
       
        System.out.println(" After remove set lhs is = "+lhs);
       
    }
   
}


Output:

 Before remove element lhs is = [mumbai, delhi, kolkata, chandigarh, dehradun]


 After remove set lhs is = [delhi, kolkata, chandigarh]

No comments:

Post a Comment