Java Set Interface
Java Set Interface
import java.util.*;
public class SetOperations
{
public static void main(String args[])
{
Integer[] A = {22, 45,33, 66, 55, 34, 77};
Integer[] B = {33, 2, 83, 45, 3, 12, 55};
Set<Integer> set1 = new HashSet<Integer>();
set1.addAll(Arrays.asList(A));
Set<Integer> set2 = new HashSet<Integer>();
set2.addAll(Arrays.asList(B));
Addall() method:
The addAll() method appends all the elements of the specified collection to
the set.
import java.io.*;
import java.util.*;
class addAllMethod {
public static void main(String args[])
{
Set<Integer> data = new LinkedHashSet<Integer>();
data.add(31);
data.add(21);
data.add(41);
System.out.println("Set: " + data);
ArrayList<Integer> newData = new ArrayList<Integer>();
newData.add(91);
newData.add(71);
newData.add(81);
data.addAll(newData);
System.out.println("Set: " + data);
} }
Output:
Clear () method: The method removes all the elements from the set. It
doesn't delete the reference of the set. It only deletes the elements of
the set.
import java.io.*;
import java.util.*;
public class clearMethod {
public static void main(String args[])
{
Set<Integer> data = new LinkedHashSet<Integer>();
data.add(31);
data.add(21);
data.add(41);
System.out.println("Set: " + data);
data.clear();
System.out.println("The final set: " + data);
}
}
Output:
Contains() method: The contains() method is used to know the presence of
an element in the set. Its return value is true or false depending on the
presence of the element.
import java.io.*;
import java.util.*;
class containsMethod {
public static void main(String args[])
{
Set<Integer> data = new LinkedHashSet<Integer>();
data.add(31);
data.add(21);
data.add(41);
data.add(51);
data.add(11);
data.add(81);
System.out.println("Set: " + data);
System.out.println("Does the Set contains '91'?" + data.contains(91
));
System.out.println("Does the Set contains 'javaTpoint'? " + data.con
tains("4"));
System.out.println("Does the Set contains '51'? " + data.contains(5
1));
}
}
Output:
5) containsAll()
The method is used to check whether all the elements of the collection are
available in the existing set or not. It returns true if all the elements of the
collection are present in the set and returns false even if one of the elements
is missing in the existing set.
Syntax:
1. public boolean containsAll(Collection data)
SetExample7.java
import java.io.*;
import java.util.*;
class containsAllMethod {
public static void main(String args[])
{
Set<Integer> data = new LinkedHashSet<Integer>();
data.add(31);
data.add(21);
data.add(41);
data.add(51);
data.add(11);
data.add(81);
} }
Output:
6) hashCode()
The method is used to derive the hash code value for the current instance of
the set. It returns hash code value of integer type.
Syntax:
1. public int hashCode()
SetExample8.java
import java.io.*;
import java.util.*;
class hashCodeMethod {
public static void main(String args[])
{
Set<Integer> data = new LinkedHashSet<Integer>();
data.add(31);
data.add(21);
data.add(41);
data.add(51);
data.add(11);
data.add(81);
System.out.println("data: " + data);
System.out.println("\nThe hash code value of set is:"+ data.hashCo
de());
}
}
Output:
7) isEmpty()
The isEmpty() method is used to identify the emptiness of the set . It returns
true if the set is empty and returns false if the set is not empty.
Syntax:
1. boolean isEmpty()
SetExample9.java
import java.io.*;
import java.util.*;
class isEmptyMethod {
public static void main(String args[])
{
Set<Integer> data = new LinkedHashSet<Integer>();
data.add(31);
data.add(21);
data.add(41);
data.add(51);
data.add(11);
data.add(81);
System.out.println("data: " + data);
System.out.println("\nIs data empty?: "+ data.isEmpty());
}
}
Output:
8) iterator()
The iterator() method is used to find the iterator of the set. The iterator is
used to get the element one by one.
Syntax:
1. Iterator iterate_value = set1.iterator();
SetExample10.java
import java.io.*;
import java.util.*;
class iteratorMethod {
public static void main(String args[])
{
Set<Integer> data = new LinkedHashSet<Integer>();
data.add(31);
data.add(21);
data.add(41);
data.add(51);
data.add(11);
data.add(81);
System.out.println("data: " + data);
Iterator newData = data.iterator();
System.out.println("The NewData values are: ");
while (newData.hasNext()) {
System.out.println(newData.next());
}
}
}
Output:
9) remove()
The method is used to remove a specified element from the Set. Its return
value depends on the availability of the element. It returns true if the element
is available in the set and returns false if it is unavailable in the set.
Syntax:
1. boolean remove(Object O)
SetExample11.java
import java.io.*;
import java.util.*;
class removeMethod {
public static void main(String args[])
{
Set<Integer> data = new LinkedHashSet<Integer>();
data.add(31);
data.add(21);
data.add(41);
data.add(51);
data.add(11);
data.add(81);
System.out.println("data: " + data);
data.remove(81);
data.remove(21);
data.remove(11);
System.out.println("data after removing elements: " + data);
}
}
Output:
11) removeAll()
The method removes all the elements of the existing set from the specified
collection.
Syntax:
1. public boolean removeAll(Collection data)
SetExample12.java
import java.io.*;
import java.util.*;
class removeAllMethod {
public static void main(String args[])
{
Set<Integer> data = new LinkedHashSet<Integer>();
data.add(31);
data.add(21);
data.add(41);
data.add(91);
data.add(71);
data.add(81);
System.out.println("data: " + data);
data.removeAll(newData);
System.out.println("data after removing Newdata elements : " + dat
a);
}
}
Output:
11) retainAll()
The method retains all the elements from the set specified in the given
collection.
Syntax:
1. public boolean retainAll(Collection data)
SetExample13.java
import java.io.*;
import java.util.*;
class retainAllMethod {
public static void main(String args[])
{
Set<Integer> data = new LinkedHashSet<Integer>();
data.add(31);
data.add(21);
data.add(41);
data.add(91);
data.add(71);
data.add(81);
System.out.println("data: " + data);
data.retainAll(newData);
System.out.println("data after retaining newdata elements : " + data
);
}
}
Output:
12) size()
The method returns the size of the set.
Syntax:
1. int size()
SetExample14.java
import java.io.*;
import java.util.*;
class sizeMethod {
public static void main(String args[])
{
Set<Integer> data = new LinkedHashSet<Integer>();
data.add(31);
data.add(21);
data.add(41);
data.add(91);
data.add(71);
data.add(81);
System.out.println("data: " + data);
13) removeAll()
The method is used to create an array with the same elements of the set.
Syntax:
1. Object[] toArray()
SetExample15.java
import java.io.*;
import java.util.*;
class toArrayMethod {
public static void main(String args[])
{
Set<Integer> data = new LinkedHashSet<Integer>();
data.add(31);
data.add(21);
data.add(41);
data.add(91);
data.add(71);
data.add(81);
System.out.println("data: " + data);