Java LinkedHashSet: add() and size() methods
KW.java
import java.util.LinkedHashSet;

class KW 
{
    public static void main(String[] args) 
    {
        //LinkedHashSet class implements the Set interface
        LinkedHashSet<String> lhs=new LinkedHashSet<>();
        lhs.add("Apple");
        lhs.add("Orange");
        lhs.add("Mango");
        lhs.add("Grapes");
        lhs.add("Cherry");
        lhs.add("Apple");
        System.out.println(lhs);
        System.out.println("Size: "+lhs.size());
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW [Apple, Orange, Mango, Grapes, Cherry] Size: 5 kodingwindow@kw:~$
Java LinkedHashSet: addAll() and containsAll() methods
KW.java
import java.util.LinkedHashSet;

class KW 
{
    public static void main(String[] args) 
    {
        //LinkedHashSet class implements the Set interface
    	LinkedHashSet<String> lhs1=new LinkedHashSet<>();
    	lhs1.add("Apple");
        lhs1.add("Orange");
        lhs1.add("Mango");
        lhs1.add("Blueberry");
        System.out.println(lhs1);
        
        LinkedHashSet<String> lhs2=new LinkedHashSet<>();
    	lhs2.add("Apple");
        lhs2.add("Orange");
        lhs2.add("Mango");
        lhs2.add("Pineapple");
        System.out.println(lhs2);
        
        lhs1.addAll(lhs2);
        System.out.println(lhs1);
        
        System.out.println(lhs1.contains("Pineapple"));
        System.out.println(lhs2.containsAll(lhs1));
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW [Apple, Orange, Mango, Blueberry] [Apple, Orange, Mango, Pineapple] [Apple, Orange, Mango, Blueberry, Pineapple] true false kodingwindow@kw:~$
Java LinkedHashSet: remove() and removeAll() methods
KW.java
import java.util.LinkedHashSet;

class KW 
{
    public static void main(String[] args) 
    {
        //LinkedHashSet class implements the Set interface
    	LinkedHashSet<String> lhs=new LinkedHashSet<>();
        lhs.add("Apple");
        lhs.add("Orange");
        lhs.add("Mango");
        lhs.add("Grapes");
        lhs.add("Cherry");
        
        for(String s:lhs)
        {
            System.out.println(s);
        }
        lhs.remove("Apple");
        System.out.println("\n"+lhs);
        
        lhs.removeAll(lhs); //lhs.clear();
        System.out.println("Size: "+lhs.size());
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Apple Orange Mango Grapes Cherry [Orange, Mango, Grapes, Cherry] Size: 0 kodingwindow@kw:~$
Advertisement