Set interface in java

Set interface:

A set represents a group of elements which can’t contain a duplicate element. It extends the collection interface.

 Note: Set interface contains only methods inherited from Collection interface and adds the restriction that can’t contain a duplicate element.

 Commonly Used methods of Set interface:

1. add(Object obj): Add object to the calling collection. It returns true if collection changed after this call otherwise returns false.

Syntax: public boolean add(Object obj)

2. addAll(Collection c): Add all objects to the calling collection. It returns true if collection changed after this call otherwise returns false.

Syntax: public boolean addAll(Collection c)

3. clear(): Remove all elements from the calling collection.

Syntax: public void clear()

4. contains(Object obj): It returns true if this collection contains the specified object otherwise returns false.

Syntax: public boolean contains(Object obj)

5. containsAll(Collection c):It returns true if this collection contains all elements of the specified collection otherwise returns false.

Syntax: public boolean containsAll(Collection c)

6. equals(Object obj): It returns true if this collection equals to the specified object otherwise returns false.

Syntax: public boolean equals(Object obj)

7. hashCode(): It returns the hash code of the calling collection.

Syntax: public int hashCode()

8. isEmpty(): It returns true if the calling collection is empty otherwise returns false.

Syntax: public boolean isEmpty()

9. iterator(): It returns an iterator instance for the calling collection.

Syntax: public Iterator iterator()

10. remove(Object obj): It remove the specified object from the calling collection. It returns true if collection changed after this call otherwise returns false.

Syntax: public boolean remove(Object obj)

11. removeAll(Collection c): It remove all elements of the specified collection from the calling collection. It returns true if collection changed after this call otherwise returns false.

Syntax: public boolean removeAll(Collection c)

12. retainAll(Collection c): It remove all elements except those of the specified collection from the calling collection. It returns true if collection changed after this call otherwise returns false.

Syntax: public boolean retainAll(Collection c)

13. size(): It returns the number of elements in the calling collection.

Syntax: public int size()

14. toArray( ): It returns an array of containing all elements of the calling collection.

Syntax: public Object[ ] toArray()

 A simple example of HashSet class to explain few methods of Set interface.

 HashSetTest.java

import

java.util.HashSet

;

import

java.util.Iterator

;

import

java.util.Set

;

 

/** * This class is used to show the HashSet functionality. * @author w3spoint */

public

class

HashSetTest

{

public

static

void

main

(

String

args

[

]

)

{

//Create HashSet object.

Set

hashSet

=

new

HashSet

(

)

;

 

//Add objects to the HashSet.

hashSet.

add

(

"Roxy"

)

;

hashSet.

add

(

"Sunil"

)

;

hashSet.

add

(

"Sandy"

)

;

hashSet.

add

(

"Munish"

)

;

hashSet.

add

(

"Pardeep"

)

;

 

//Print the HashSet object.

System

.

out

.

println

(

"HashSet elements:"

)

;

System

.

out

.

println

(

hashSet

)

;

 

//Print the HashSet elements using iterator.

Iterator

iterator

=

hashSet.

iterator

(

)

;

System

.

out

.

println

(

"HashSet elements using iterator:"

)

;

while

(

iterator.

hasNext

(

)

)

{

System

.

out

.

println

(

iterator.

next

(

)

)

;

}

}

}

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set; /**
* This class is used to show the HashSet functionality.
* @author w3spoint
*/
public class HashSetTest {
public static void main(String args[]){
//Create HashSet object.
Set hashSet = new HashSet();
//Add objects to the HashSet.
hashSet.add(“Roxy”);
hashSet.add(“Sunil”);
hashSet.add(“Sandy”);
hashSet.add(“Munish”);
hashSet.add(“Pardeep”);
//Print the HashSet object.
System.out.println(“HashSet elements:”);
System.out.println(hashSet);
//Print the HashSet elements using iterator.
Iterator iterator=hashSet.iterator();
System.out.println(“HashSet elements using iterator:”);
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}

Output:

HashSet

elements

:

[

Sandy, Pardeep, Munish, Sunil, Roxy

]

HashSet

elements using iterator

:

Sandy Pardeep Munish Sunil Roxy

HashSet elements:
[Sandy, Pardeep, Munish, Sunil, Roxy]
HashSet elements using iterator:
Sandy
Pardeep
Munish
Sunil
Roxy

Download this example.
 
Next Topic: SortedSet interface in java with example.
Previous Topic: Collection interface in java with example.

Please Share