Sets
Set in Java
Set is an interface which extends Collection. It is an unordered collection of objects in which duplicate values cannot be stored.
Basically, Set is implemented by HashSet, LinkedHashSet or TreeSet (sorted representation).
Set has various methods to add, remove clear, size, etc to enhance the usage of this interface
Basic Set operation :
Union In this, we could simply add one Set with other. Since the Set will itself not allow any duplicate entries, we need not take care of the common values.
Expected Output:
Intersection We just need to retain the common values from both Sets.
Expected Output:
Difference We just need to remove all the values of one Set from the other. Expected Output:
Problems:
1. Write a Java program to append the specified element to the end of a hash set. Go to the editor Click me to see the solution
2. Write a Java program to iterate through all elements in a hash list. Go to the editor Click me to see the solution
3. Write a Java program to get the number of elements in a hash set. Go to the editor Click me to see the solution
4. Write a Java program to empty an hash set. Go to the editor Click me to see the solution
5. Write a Java program to test a hash set is empty or not. Go to the editor Click me to see the solution
6. Write a Java program to clone a hash set to another hash set. Go to the editor Click me to see the solution
7. Write a Java program to convert a hash set to an array. Go to the editor Click me to see the solution
8. Write a Java program to convert a hash set to a tree set. Go to the editor Click me to see the solution
9. Write a Java program to convert a hash set to a List/ArrayList. Go to the editor Click me to see the solution
10. Write a Java program to compare two hash set. Go to the editor Click me to see the solution
11. Write a Java program to compare two sets and retain elements which are same on both sets. Go to the editor Click me to see the solution
12. Write a Java program to remove all of the elements from a hash set. Go to the editor Click me to see the solution
CC:
Last updated