Menu
Topics Index
...
`


Collections Framework > Collection Interface >
Siva Nookala - 15 Apr 2016
A Set Interface is a Collection that prevents duplicate elements. Only the unique elements will be added to the set. The order in which the elements are added is not preserved.

The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Two Set instances are equal if they contain the same elements. As implied by its name, this interface models the mathematical set abstraction.

Set Interface Methods:
MethodDesription
boolean add(element)Adds the specified element to this set if it is not already present.
void clear()Removes all of the elements from this set.
boolean contains(element)Returns true if this set contains the specified element.
boolean isEmpty()Returns true if this set contains no elements.
Iterator iterator()Returns an iterator over the elements in this set.
boolean remove(element)Removes the specified element from this set if it is present.
int size()Returns the number of elements in this set.
higher(E obj)Searches the set for the largest element E such that E > obj. If such an element is found, it is returned. Otherwise, null is returnd.
Lower(E obj)Searches the set for the largest element E such that E < obj. If such an element is found, it s returned. Otherwise, null is returned.

Set Interface
import java.util.*;

class SetTest
{
    public static void main(String arg[])
    {
        Set<Integer> s = new HashSet<Integer>();
        s.add(10);
        s.add(30);
        s.add(98);
        s.add(80);
        s.add(99);
        s.add(80); // Duplicate
        
        System.out.println("Set elements : " + s);
        System.out.print("Iterating set elements : ");
        Iterator it = s.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }
        System.out.println();
        
        System.out.println("Size of Set is : " + s.size());
        System.out.println("Set contains element : " + s.contains(30));
        System.out.println("Set is empty : " + s.isEmpty());    
    }
}
OUTPUT

Set elements : [98, 99, 80, 10, 30]
Iterating set elements : 98 99 80 10 30
Size of Set is : 5
Set contains element : true
Set is empty : false

DESCRIPTION

In this program, a HashSet is assigned to Set object, six elements are added to it in which one is duplicate and then set methods like Iterator , size, contains and isEmpty are applied.

THINGS TO TRY
  • Add 30 to the set, the output do not differ. It do not take any duplicates.
  • Add 50 to the set and see the output difference, where 50 will be added to set and the size of set will be increased by 1.
  • Copy paste this set
    Set<String> s = new HashSet<String>();
    s.add("Seeta");
    s.add("Geeta");
    s.add("Reeta");
    s.add("Meena");
    s.add("Geeta");// Duplicate
    s.add("Reeta");// Duplicate
    in place of Integer Set and see the output.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App