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.

 public interface Set extends Collection

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

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

public class LinkedHashSet extends HashSet implements Set

public class TreeSet extends AbstractSet implements SortedSet

public class HashSet extends AbstractSet implements Set

public abstract class AbstractSet extends AbstractCollection implements Set

public interface SortedSet extends Set

public interface Set extends Collection

public interface Collection

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

Related Posts

  • Abstract Class In JAVA

    Hello Friends, This tutorial is for all the Java followers. One of the best feature that is widely used is the term ‘Abstract’. This term can be used as either class or a simple method. An abstract method is any method that is just declared but not instantiated. In other words one can just create…

  • Threads in Java.

    Hello Friends, This is the tutorial for the java developers. One of the most significance feature of core java is Threading. Threading deals with the processing of Threads in a single java program. Let us learn what actually are Threads. *What are Threads? Threads are independently running processes that are isolated from each other upto…

  • Collections In Java.

    Hello friends, Welcome to another tutorial for java followers. You all may have heard about Collections, it is one of the amazing feature in java. Collections are the object for the group of elements, these elements are nothing but the different data structures like as Array Lists, Linked Lists, Vectors, Hash tables,Hash List, Trees, Hash…

  • Maven Installation.

    Hello Friends, This is one of my tutorial for installation of Maven on windows. Maven is a software project management tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. Maven is an open source community and is a…

  • Jira Plugin Deployment.

    Hello Friends, This is one of the most important tutorial for all the jira developers. You may have heard about the plugins in jira which is a simple Java Archive File with .jar extension. This JAR file contains the class files and auxiliary resources associated with the applications. In Jira we can use Eclipse IDE…

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.