Sunday, October 26, 2014

Java Program to demonstrate Set


SetExample.java:

import java.util.*;

public class SetExample {

   public static void main(String args[]) {

       // We create a new, empty set
       Set mySet1 = new HashSet();
       // We add a few elements
       mySet1.add("A");
       mySet1.add("C");
       mySet1.add("A");
       mySet1.add("B");
       // Print the elements of the Set
       System.out.println("mySet1: " + mySet1);

       // Create a list and add some elements
       List list = new ArrayList();
       list.add("A");
       list.add("C");
       list.add("A");
       list.add("A");
       list.add("B");
       list.add("C");
       // Now create the set using the appropriate constructor
       Set mySet2 = new HashSet(list);
       // Print the elements of the list an the the set
       System.out.println("list: " + list);
       System.out.println("mySet2: " + mySet2);

       // Compare the two sets
       System.out.println("MySet1 matches mySet2: " + mySet1.equals(mySet2));

       // Now we will remove one element from mySet2 and compare again
       mySet2.remove("A");
       System.out.println("mySet2: " + mySet2);
       System.out.println("MySet1 matches mySet2: " + mySet1.equals(mySet2));

       // Lets check if our sets contain all the elements of the list
       System.out.println("MySet1 contains all the elements: " + mySet1.containsAll(list));
       System.out.println("MySet2 contains all the elements: " + mySet2.containsAll(list));

       // Use of Iterator in Set
       Iterator iterator = mySet1.iterator();
       while (iterator.hasNext()) {
           System.out.println("Iterator loop: " + iterator.next());
       }

       // Use of for-each in Set
       for (String str : mySet1) {
           System.out.println("for-each loop: " + str);
       }

       // Clearing all the elements
       mySet1.clear();
       System.out.println("mySet1 is Empty: " + mySet1.isEmpty());

       // Checking the number of elements
       System.out.println("mySet1 has: " + mySet1.size() + " Elements");
       System.out.println("mySet2 has: " + mySet2.size() + " Elements");

       // Creating an Array with the contents of the set
       String[] array = mySet2.toArray(new String[mySet2.size()]);
       System.out.println("The array:" + Arrays.toString(array));
   }
}
Output:
mySet1: [A, B, C]
list: [A, C, A, A, B, C]
mySet2: [A, B, C]
MySet1 matches mySet2: true
mySet2: [B, C]
MySet1 matches mySet2: false
MySet1 contains all the elements: true
MySet2 contains all the elements: false
Iterator loop: A
Iterator loop: B
Iterator loop: C
for-each loop: A
for-each loop: B
for-each loop: C
mySet1 is Empty: true
mySet1 has: 0 Elements
mySet2 has: 2 Elements
The array:[B, C]

2 comments: