Set is the group of elements, or we can say collection of elements, that can never contains Duplicate element. If in case duplicate elements is added then Set Interface will not add it in that group of elements. Set also allow null element and as it avoids duplicity, so only one null is allowed.
The formal definition of the Set interface states that no two elements in the Set are equal such that e1.equals(e2) is never true.
Table of Contents
Set Interface extends Collection Interface which further extends Iterable Interface, as shown in figure 1.
1. HashSet
HashSet is the implementation of Set Interface which uses Hash table for storing the data. Hash table internally uses a phenomena known as hashing, Hash set does not maintains the insertion order, that is when we retrieve values from it we do not get that values in the same order as we have entered in it.
2. LinkedHashSet
LinkedHashSet is also the implementation of Set Interface. It is almost similar to hashset, but it maintains insertion order i.e. values are retrieved in the same order in which they are added. It uses doubly linked list to obtain this functionality.
For more read LinkedHashSet tutorial
3. TreeSet
TreeSet also implements Set Interface, it is also similar to hashset , but it stores all the elements in their natural order , like all integer values are stored in ascending order and strings are stored according to Dictionary values.
Apart from adding this functionality of maintaining natural ordering, TreeSet do not allow null values.
TreeSet is best choice for storing large amount of data, as its retrieval and access time is very fast, which makes data to be found in no time.
For more read TreeSet tutorial
Let us discuss Set Interface with the help of program; following program has been divided into 3 Steps that we will discuss one by one.
import java.util.*; public class SetInterfaceDemo{ public static void main(String args[]){ Set<Integer> hashSetobject= new HashSet<Integer>(); //Step 1 Set<Integer> linkedHashSetObject = new LinkedHashSet<Integer>(); Set<Integer> treeSetObject= new TreeSet<Integer>(); hashSetobject.add(1); //Step 2 hashSetobject.add(2); hashSetobject.add(4); linkedHashSetObject.add(3); linkedHashSetObject.add(1); linkedHashSetObject.add(2); linkedHashSetObject.add(4); treeSetObject.add(3); treeSetObject.add(1); treeSetObject.add(2); treeSetObject.add(4); System.out.println("HashSet object is : " +hashSetobject); //Step 3 System.out.println("LinkedHashSet object is : " +linkedHashSetObject); System.out.println("TreeSet object is : " +treeSetObject); } }
Output:
HashSet object is : [1, 2, 4] LinkedHashSet object is : [3, 1, 2, 4] TreeSet object is : [1, 2, 3, 4]
1. boolean add(Object)
This methods adds an element to the set, if and only if that element is not already present in the set, It will return true if element is added, or false if given element is already present and hence it is not added
2. void clear()
This method removes all elements from the Set.
3. boolean contains(Object)
This method returns true if the Set contains the given element in the set, else false if there is no such element.
4. boolean isEmpty()
This method returns true if the Set contains no elements or false if there are elements present in the set.
5. Iterator iterator()
This method gives an object of Iterator class which can be used to traverse through the Set.
6. boolean remove(Element e)
This method removes the given element in the argument from the Set and returns true if the argument was removed, else false if the given element is not present.
7. int size()
This method gives the number of elements in the Set.
8. Object[] toArray()
This methods returns the set in the form of array of type “Object
Premium Project Source Code:
Leave a Reply