Set Interface

Posted by

Set interface extends Collection in java, unlike any other implementation of Collection interface, Set implemention does not allow duplicate element.It also means a set can contain at most one null value.It does not define any of its own method.
Subinterface – SortedSet
Classes which implements Set Interface- AbstractSet, HashSet, LinkedHashSet, TreeSet,

Methods-

The methods which are given below are inherited from Collection interface.

Method Description
boolean add(Object obj) Adds the specified element to the set if it is not already present and return true otherwise return false.
boolean addAll(Collection c) Adds all the elements of c to the invoking collection. Returns true if the operationsucceeded (i.e., the elements were added).Otherwise, returns false.
void clear() Removes all of the elements from the set.
boolean contains(Object o) Returns true if this set contains the specified element.
boolean contains(Object o) Returns true if obj is an element of the invoking set. Otherwise, returns false.
boolean containsAll(Collection c) Returns true if the invoking set contains all elements of c. Otherwise, returns false.
boolean equals(Object obj) Returns true if the invoking set and obj are equal. Otherwise, returns false.
int hashCode() Returns the hash code for the invoking set.
boolean isEmpty() Returns true if the invoking collection is empty. Otherwise, returns false.
Iterator iterator() Returns an iterator over the elements in this set.
boolean remove(Object obj) Removes the specified element from this set if it is present.
boolean removeAll(Collection c) Removes from this set all of its elements that are contained in the specified collection .
boolean retainAll(Collection c) Retains only the elements in this set that are contained in the specified collection.
int size( ) Returns the number of elements in this set.
Object[ ] toArray( ) Returns an array containing all of the elements in this set.
Object[ ] toArray(Object array[ ]) Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.

One comment

Leave a Reply