Important Key Points about the Set Interface
Unique Elements:
Ordering:
Null Elements:
Methods:
Subinterfaces and Implementations:
No Position Access:
Use-cases:
Equality:
Thread Safety:
Set Interface Hierarchy Diagram
Set Interface APIs/Methods
- boolean add(E e) - This method adds the specified element to this set if it is not already present (optional operation).
- boolean addAll(Collection<? extends E> c) - This method adds all of the elements in the specified collection to this set if they're not already present (optional operation).
- _void clear() _- This method removes all of the elements from this set (optional operation).
- boolean contains(Object o) - This method returns true if this set contains the specified element.
- boolean containsAll(Collection<?> c) - This method returns true if this set contains all of the elements of the specified collection.
- boolean equals(Object o) - This method compares the specified object with this set for equality.
- int hashCode() - This method returns the hash code value for this set.
- boolean isEmpty() - This method returns true if this set contains no elements.
- Iterator iterator() - This method returns an iterator over the elements in this set.
- boolean remove(Object o) - This method removes the specified element from this set if it is present (optional operation).
- boolean removeAll(Collection<?> c) - This method removes from this set all of its elements that are contained in the specified collection (optional operation).
- boolean retainAll(Collection<?> c) - This method retains only the elements in this set that are contained in the specified collection (optional operation).
- int size() - This method returns the number of elements in this set (its cardinality).
- default Spliterator spliterator() - This method creates a Spliterator over the elements in this set.
Example 1: Set Interface with Its HashSet Implementation Class
import java.util.HashSet;
import java.util.Set;
public class CreateHashSetExample {
public static void main(String[] args) {
// Creating a HashSet
Set<String> daysOfWeek = new HashSet<>();
// Adding new elements to the HashSet
daysOfWeek.add("Monday");
daysOfWeek.add("Tuesday");
daysOfWeek.add("Wednesday");
daysOfWeek.add("Thursday");
daysOfWeek.add("Friday");
daysOfWeek.add("Saturday");
daysOfWeek.add("Sunday");
// Adding duplicate elements will be ignored
daysOfWeek.add("Monday");
System.out.println(daysOfWeek);
}
}
[Monday, Thursday, Friday, Sunday, Wednesday, Tuesday, Saturday]Example 2: Set Interface with Its LinkedHashSet Implementation Class
// Creating a HashSet
LinkedHashSet<String> daysOfWeek = new LinkedHashSet<>();
// Adding new elements to the HashSet
daysOfWeek.add("Monday");
daysOfWeek.add("Tuesday");
daysOfWeek.add("Wednesday");
daysOfWeek.add("Thursday");
daysOfWeek.add("Friday");
daysOfWeek.add("Saturday");
daysOfWeek.add("Sunday");
// Adding duplicate elements will be ignored
daysOfWeek.add("Monday");
System.out.println(daysOfWeek);
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]
Read more about LinkedHashSet class with examples at Guide to LinkedHashSet Class.
Example 3: Set Interface with Its TreeSet Implementation Class
// Creating a TreeSet
TreeSet<String> fruits = new TreeSet<>();
// Adding new elements to a TreeSet
fruits.add("Banana");
fruits.add("Apple");
fruits.add("Pineapple");
fruits.add("Orange");
System.out.println("Fruits Set : " + fruits);
// Duplicate elements are ignored
fruits.add("Apple");
System.out.println("After adding duplicate element \"Apple\" : " + fruits);
// This will be allowed because it's in lowercase.
fruits.add("banana");
System.out.println("After adding \"banana\" : " + fruits);
Fruits Set : [Apple, Banana, Orange, Pineapple]
After adding duplicate element "Apple" : [Apple, Banana, Orange, Pineapple]
After adding "banana" : [Apple, Banana, Orange, Pineapple, banana]
Set Interface Implementation Classes
There are three general-purpose Set implementations:Related Collections Framework Interfaces
- Collections Framework - The Collection Interface
- Collections Framework - The Set Interface
- Collections Framework - The SortedSet Interface
- Collections Framework - The List Interface
- Collections Framework - The Queue Interface
- Collections Framework - The Deque Interface
- Collections Framework - The Map Interface
- Collections Framework - The SortedMap Interface
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner


Comments
Post a Comment
Leave Comment