Collections in Java Part II : Sets

Hello Friends,

This is part II for the collections tutorial, earlier we discussed the basics of collection framework. In this part,we will look at the one of the child interface, the SET interface.

A Set is the collection that contains no duplicate elements. Any class implementing the Set interface is not permitted to have duplicate entries, to be more precised, no two elements in the Set s1 ans s2 are such that s1.equals(s2). The Set interface extends the super interface – Collection interface, however there is one subInterface of Set called as SortedSet.

for E.g.

It is implemented using the classes AbstractSet, HashSet, LinkedHashSet, TreeSet.

Now, the above can be summarized as per their implementations as:-

However, there are number of methods that can be used with Sets, following is the summary for the methods:-

1) add : boolean add(Object o)
-> Adds the specified element to the set only if it is not already present. If the element is already available then no element is added and hence Set remains unchanged returning false.

2) addAll : boolean addAll(Collection c)
-> Adds all of the elements in the specified collection to the set if they’re not already present, similar to add.

3) clear :  void clear()
-> Removes all of the elements from the set or make the set empty.

4) contains: boolean contains(Object o)
-> Used to check the specific element in the set, returns true if the set contains the specified element.

5) containsAll: boolean containsAll(Collection c)
-> It is similar to contains, returns true if the set contains all of the elements of the specified collection.

6) equals:  boolean equals(Object o)
-> Compares the specified object with this set for equality.

7) hashCode:  int hashCode()
-> Returns the hash code value for this set.

8 ) isEmpty: boolean isEmpty()
-> Returns true if this set is empty or contains no elements.

9) iterator; iterator()
-> Used to iterate the set elements one by one.

10) remove:  boolean remove(Object o)
-> Removes the specified element from the set if it is present.

11) removeAll;  boolean removeAll(Collection c)
-> Removes all the elements from the set that are contained in the specified collection.

12) size: int size()
-> Returns the size of the set i.e. total number of elements in the set.

13) toArray: Object[] toArray()
-> Returns an array containing all of the elements in the set.

However, there are number of methods that can be used with Sets, following is the summary for the methods:-

Please check this example:-

[java]

package test;

import java.util.*;

public class SetExample
{
public static void main(String args[])
{
Set set = new HashSet();

//Adding elements in HashSet
set.add(“one”);
set.add(“two”);
set.add(“three”);
set.add(“two”);
set.add(“five”);
set.add(“six”);
set.add(“100”);
set.add(“20”);
System.out.println(“Values in HashSet” + set);

//Using TreeSet
Set sortedSet = new TreeSet();

//Adding elements in TreeSet
sortedSet.addAll(set);
System.out.println(“Tree Set Values are Sorted : ” + sortedSet);

//Adding elements in TreeSet
sortedSet.add(“101”);
System.out.println(“Tree Set after addition : “+sortedSet);

// Using LinkedHashSet
Set linkset = new LinkedHashSet();

//Adding all elements from set to LinkedHashSet
linkset.addAll(set);
System.out.println(“Link set Values are : “+linkset);

//Using removeAll method will remove all the contents in linkset
linkset.removeAll(linkset);
System.out.println(“Link set aftre removeAll : “+linkset);

}
}

[/java]

Run the program and check the output. Hope this will help you.

Thanks,

admin@code2java.com

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.