set interface java

Learn Set Interface in Java with Programming Example

set interface javaIn this chapter you will learn:

What is Set Interface in Java?
Set Interface Classes
Programming Example

What is Set Interface in Java?

A Set is a collection which doesn’t allow adding duplicate entry in the list. It models the mathematical set abstraction. It contains methods only inherited from collection and adds restriction that duplicate elements are prohibited. Two set interfaces are equal if there elements are same.

Types of Set

There are three general purpose set implements in java that is listed below:

  1. HashSet It stores elements in hash table but orders of stored elements are not guaranteed.
  2. TreeSet It stores elements in red-black tree and orders of stored elements are based on their values. It is quite slower than HashSet.
  3. LinkedHashSet It stores elements in hash table with linked list and orders of stored elements are same as they inserted.

Methods

Methods Descriptions
boolean add(E e) Adds the specified element to this set if it is not already present (optional operation).
boolean addAll(Collection<? extends E> c) Adds all of the elements in the specified collection to this set if they’re not already present (optional operation).
void clear() Removes all of the elements from this set (optional operation).
boolean contains(Object o) Returns true if this set contains the specified element.
boolean containsAll(Collection<?> c) Returns true if this set contains all of the elements of the specified collection.
boolean equals(Object o) Compares the specified object with this set for equality.
int hashCode() Returns the hash code value for this set.
boolean isEmpty() Returns true if this set contains no elements.
Iterator<E> iterator() Returns an iterator over the elements in this set.
boolean remove(Object o) Removes the specified element from this set if it is present (optional operation).
boolean removeAll(Collection<?> c) Removes from this set all of its elements that are contained in the specified collection (optional operation).
boolean retainAll(Collection<?> c) Retains only the elements in this set that are contained in the specified collection (optional operation).
int size() Returns the number of elements in this set (its cardinality).
Object[] toArray() Returns an array containing all of the elements in this set.
T[] toArray(T[] a) Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.

Declaration:

Set s=new HashSet();

Programming Example

import java.util.*;
class Set_Example
{
  public static void main(String[] args)
  {
    Set s=new HashSet();
    s.add("PHP");
    s.add("Java");
    s.add("SQL");
    s.add("HTML");
    //Printing Hashset
    System.out.println(s);
    
    //Sorting Set in alphabetic order
    TreeSet st=new TreeSet(s);
    System.out.println(st);
    
    //Printing First Element
    System.out.println("First Element : " + st.first());
    
    //Printing Last Element
    System.out.println("Last Element : " + st.last());
    
  }    
}

Output

D:\JavaProgram>javac Set_Example.java
D:\JavaProgram>java Set_Example[SQL, PHP, HTML, Java]
[HTML, Java, PHP, SQL]
First Element : HTML
Last Element : SQL
_

Summary

In this chapter you saw quick review of Set Interface in java. In the next chapter you will learn HashSet Class in Java.


Leave a Reply

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