Updated March 29, 2023
Introduction to Java collection set
Collection is nothing but a framework in java that enables users to store and manipulate a group of objects. Java collection framework supports numerous interfaces and set is one collection interface. It collects unordered elements wherein duplicate values are not stored. Java collection set interface eliminates the duplicate values. It is implemented by TreeSet, HashSet, or LinkedHashSet. While iterating the set, each of these implementations can act differently with respect to the ordering of the elements.
Syntax:
Below is the syntax which is used to create the collection set in java. Set num = new HashSet();
Here, Set is a keyword that is used to create a set interface. An integer is a data type. Based on the requirement we can use a suitable data type. num is a variable that stores the values. new is keyword. HashSet is an interface, we can also use another interface like LinkedHashSet or TreeSet, based on the requirements.
Working of Java set collection
Inset collection, we can perform various operations like
- add(): It is used to insert a new value in the set.
- addAll(): It is used to append all the elements of the specified collection to a set.
- clear(): It is used to clear all the elements from the set.
- remove(): It is used to remove the specified value from the set.
- contains(): It is used to determine the presence of the element in the set.
- hashCode(): It is used to calculate the hash code value of the set.
- isEmpty(): It is used to determine the emptiness of the set. If the set is empty it returns true else it will return false.
- size: It is used to calculate the size of the set.
Examples of Java collection set
Here we are going to discuss some examples to understand the implementation of collection set in Java
Example #1
Implementation of the Set interface in java with add method.
Code:
import java.util.*;
public class Main{
public static void main(String[] args)
{
// create LinkedHashSet using the Set
Set example = new LinkedHashSet();
example.add("Welcome");
example.add("to");
example.add("eduCBA");
example.add("Java Collection Set Example");
System.out.println(example);
}
}
Output:
Explanation
Here, we have written a program to add the the values in the set. First, we have created a set using LinkedHashSet. And then with the help of add method, we have added the multiple strings in a single set and print that set using System.out.println.
Example #2
Implementation of the set interface in java with the clear method.
Code:
import java.util.*;
public class Main{
public static void main(String[] args)
{
// creating LinkedHashSet using the Set
Set example = new LinkedHashSet();
example.add("Welcome");
example.add("to");
example.add("eduCBA");
example.add("Java Collection Set Example");
System.out.println(example);
example.clear();
System.out.println("Result after clear method" +example);
}
}
Output:
Explanation
Here we have written a program to clear the values from the set. As discussed in the first example we have created a set set of values and then apply the clear method. You can see that the first statement prints the set containing values and the statement after the clear method print nothing as it clear all the values.
Example #3
implementation of the set interface in java with a clear method
Code:
import java.util.*;
public class Main{
public static void main(String[] args)
{
// creating LinkedHashSet using the Set
Set example = new LinkedHashSet();
example.add(1);
example.add(2);
example.add(3);
example.add(4);
System.out.println(example);
example.clear();
System.out.println("Result after clear method" +example);
}
}
Output:
Explanation
this example is the same as the second example. The only difference is here we have created a set with numeric values.
Example #4
Implementation of the set interface in java with contains method
Code:
import java.util.*;
public class Main{
public static void main(String[] args)
{
// creating LinkedHashSet using the Set
Set example = new LinkedHashSet();
example.add(1);
example.add(2);
example.add(3);
example.add(4);
System.out.println(example);
System.out.println("Is there given set has value 5?" + example.contains(5));
System.out.println("Is there given set has value 2?" + example.contains(2));
}
}
Output:
Explanation
contains method is used to check whether the mentioned value is present in the set or not. It returns the result in a boolean format like true or false. Here in this example, the first statement returns a false value as 5 is not there in the set and the second statement returns a true value as 2 is there in the set.
Example #5
Implementation of the set interface in java with the remove method.
Code:
import java.util.*;
public class Main{
public static void main(String[] args)
{
// creating LinkedHashSet using the Set
Set example = new LinkedHashSet();
example.add(1);
example.add(2);
example.add(3);
example.add(4);
System.out.println(example);
example.remove(2);
example.remove(4);
System.out.println("Final set after remove method" +example);
System.out.println("Is there given set has value 2?" + example.contains(2));
}
}
Output:
Explanation
Here we have written a program to implement the remove method with set collection in java. It removes the mentioned value from th set.
Conclusion
Here in this article, we have discussed the basic concepts of set collection in java. We have also discussed some examples to implement them in practice. Hope you enjoyed the article.
Recommended Articles
This is a guide to Java collection set. Here we discuss the basic concepts of kf set collection in java and also some examples for implementation. You may also have a look at the following articles to learn more –