Java program for set operation


java program for set operation

Aim: Write a java program for set operation

The set is an interface which extends Collection. It is an unordered collection of objects in which duplicate values cannot be stored. Basically, Set is implemented by Hash Set, LinkedHashSet or TreeSet (sorted representation). Set has various methods to add, remove clear, size, etc to enhance the usage of this interface

Procedure for Set Operations in Java:

  1. Set is an interface which extends Collection. It is an unordered collection of objects in which duplicate values cannot be stored.
  2. Basically, Set is implemented by HashSet, LinkedHashSet or TreeSet (sorted representation).
  3. Set has various methods to add, remove clear, size, etc to enhance the usage of this interface.

Program:

// Java code for adding elements in Set

importjava.util.*;

publicclassSet_example

{

    publicstaticvoidmain(String[] args)

    {

        // Set deonstration using HashSet

        Set<String>hash_Set = newHashSet<String>();

        hash_Set.add(“Hello”);

        hash_Set.add(“For”);

        hash_Set.add(“Hello”);

        hash_Set.add(“Example”);

        hash_Set.add(“Set”);

        System.out.print(“Set output without the duplicates”);

        System.out.println(hash_Set);

        // Set deonstration using TreeSet

        System.out.print(“Sorted Set after passing into TreeSet”);

        Set<String>tree_Set = newTreeSet<String>(hash_Set);

        System.out.println(tree_Set);

    }

}

Output:

java program for set operation

java program for set operation

https://en.wikipedia.org/wiki/Set_operations_(SQL)

https://en.wikipedia.org/wiki/Set_operation

Have any Question or Comment?

Leave a Reply

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

Categories