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

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

class KW 
{
    public static void main(String[] args) 
    {
        //HashSet class implements the Set interface
        HashSet<String> hs1=new HashSet<>();
        hs1.add("Apple");
        hs1.add("Orange");
        hs1.add("Mango");
        System.out.println(hs1);

        HashSet<String> hs2=new HashSet<>();
        hs2.add("Apple");
        hs2.add("Orange");
        hs2.add("Cherry");
        System.out.println(hs2);
        
        System.out.println(hs1.contains("Apple"));
        System.out.println(hs1.containsAll(hs2));
        
        hs1.addAll(hs2);
        System.out.println(hs1);
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW [Apple, Mango, Orange] [Apple, Cherry, Orange] true false [Apple, Cherry, Mango, Orange] kodingwindow@kw:~$
Java HashSet: remove() and removeAll() methods
KW.java
import java.util.HashSet;

class KW 
{
    public static void main(String[] args) 
    {
        //HashSet class implements the Set interface
        HashSet<String> hs=new HashSet<>();
        hs.add("Apple");
        hs.add("Orange");
        hs.add("Mango");
        hs.add("Grapes");
        hs.add("Cherry");
        hs.add("Apple");
        System.out.println(hs);
        
        hs.remove("Cherry");
        System.out.println(hs);
        
        hs.removeAll(hs);
        System.out.println(hs);
        
        //hs.clear();
        if(hs.isEmpty())
        {
            System.out.println("HashSet is empty now");
        }
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW [Apple, Grapes, Cherry, Mango, Orange] [Apple, Grapes, Mango, Orange] [] HashSet is empty now kodingwindow@kw:~$
Advertisement