Java Collections : Set Example

Hello Friends,

This is the Example for all the methods used in the java collections SET inteface, each method is implemented in the below example.

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

public class SetExample
{
     public static void main(String args[]) {
     //initialization
     Set nameSet1 = new HashSet();
     Set nameSet2 = new HashSet();
     // add() method
     nameSet1.add("a");
     nameSet1.add("b");
     nameSet1.add("c");

     nameSet2.add("a");
     nameSet2.add("b");
     nameSet2.add("c");
     nameSet2.add("d");

     //equals() method
     {
      //Checking Sets whether Identical or not
        if (nameSet2.equals(nameSet1) || nameSet1.containsAll(nameSet2))
        System.out.println("The sets are identical");
        else
        System.out.println("The sets are not identical");
      }

     //addAll() method
     nameSet1.addAll(nameSet2);
     System.out.println("The contents of Set2 is added in Set1");
     {
         //After adding, Checking Sets whether Identical or not
         if (nameSet2.equals(nameSet1) && nameSet1.containsAll(nameSet2))
         System.out.println("The sets are identical");
         else
         System.out.println("The sets are not identical");
     }

     //size() method
     int size = nameSet1.size();
     System.out.println("Size of Set1 is =" + size);

     //iterator() method
     Iterator it1 = (Iterator) nameSet1.iterator();
     System.out.print("The elements in Set 1 are as-");
     while (it1.hasNext())
    {
       Object element1 = it1.next();
       System.out.print(element1 + ", ");
     }

     // contains() method
     if (nameSet1.contains("a"))
        System.out.println("n" + "the set1 contains element a");
     else
        System.out.println("the set1 doesnot contains element a");

     //clear() method
      nameSet2.clear();
      System.out.println("The Set 2 is cleared");

     //hashcode()
     int hash = nameSet1.hashCode();
     System.out.println("the hashCode od set1 is "+hash);

     //isEmpty() method
     if (nameSet2.isEmpty())
        System.out.println("The Set 2 is empty");
     else
        System.out.println("The Set 2 is not empty");

    //toArray() method
    Object[] array = nameSet1.toArray();
    int len = array.length;
    System.out.println("Length of array is " + len);
    for (int i = 0; i < len; i++)
    {
        System.out.println("value at index " + i + " is " + array[i]);
     }

     //remove() method
     nameSet1.remove("d");
     System.out.print("The element d is removed from Set 1 and the remaining elements are as - ");
     Iterator it2 = (Iterator) nameSet1.iterator();
     while (it2.hasNext()) {
     Object element = it2.next();
     System.out.print(element + ", ");
    }
  }
}

Hope this will help you.

Thanks,

admin@code2java.com

Related Posts

  • Abstract Class In JAVA

    Hello Friends, This tutorial is for all the Java followers. One of the best feature that is widely used is the term ‘Abstract’. This term can be used as either class or a simple method. An abstract method is any method that is just declared but not instantiated. In other words one can just create…

  • Threads in Java.

    Hello Friends, This is the tutorial for the java developers. One of the most significance feature of core java is Threading. Threading deals with the processing of Threads in a single java program. Let us learn what actually are Threads. *What are Threads? Threads are independently running processes that are isolated from each other upto…

  • Collections In Java.

    Hello friends, Welcome to another tutorial for java followers. You all may have heard about Collections, it is one of the amazing feature in java. Collections are the object for the group of elements, these elements are nothing but the different data structures like as Array Lists, Linked Lists, Vectors, Hash tables,Hash List, Trees, Hash…

  • Maven Installation.

    Hello Friends, This is one of my tutorial for installation of Maven on windows. Maven is a software project management tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. Maven is an open source community and is a…

  • Jira Plugin Deployment.

    Hello Friends, This is one of the most important tutorial for all the jira developers. You may have heard about the plugins in jira which is a simple Java Archive File with .jar extension. This JAR file contains the class files and auxiliary resources associated with the applications. In Jira we can use Eclipse IDE…

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.