🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Table of Contents
- Introduction
- Why Convert List to Set?
- Methods to Convert List to Set
- Using HashSet
- Using TreeSet
- Using LinkedHashSet
- Example Code
- Conclusion
1. Introduction
In Java, List is an ordered collection that can contain duplicate elements, while Set is an unordered collection that does not allow duplicates. Converting a List to a Set can be useful in many scenarios, such as filtering out duplicates from a list.
2. Why Convert List to Set?
- Remove Duplicates: Ensure that all elements are unique.
- Efficient Membership Testing: Sets provide efficient methods for checking if an element exists.
- Natural Ordering or Insertion Order: Use
TreeSetfor natural ordering orLinkedHashSetto maintain insertion order.
3. Methods to Convert List to Set
Using HashSet
HashSet is one of the most common implementations of the Set interface. It does not maintain any order of elements.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ListToSetExample {
public static void main(String[] args) {
// Create a list with duplicate elements
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("apple");
list.add("orange");
// Convert list to set using HashSet
Set<String> set = new HashSet<>(list);
// Print the set
System.out.println("Set: " + set);
}
}
Using TreeSet
TreeSet implements the Set interface and stores elements in a sorted order. It uses the natural ordering of elements or a custom comparator.
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class ListToTreeSetExample {
public static void main(String[] args) {
// Create a list with duplicate elements
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("apple");
list.add("orange");
// Convert list to set using TreeSet
Set<String> set = new TreeSet<>(list);
// Print the set
System.out.println("Sorted Set: " + set);
}
}
Using LinkedHashSet
LinkedHashSet maintains the insertion order of elements while ensuring that all elements are unique.
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class ListToLinkedHashSetExample {
public static void main(String[] args) {
// Create a list with duplicate elements
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("apple");
list.add("orange");
// Convert list to set using LinkedHashSet
Set<String> set = new LinkedHashSet<>(list);
// Print the set
System.out.println("Ordered Set: " + set);
}
}
4. Example Code
Here is a complete example that demonstrates all three methods for converting a List to a Set.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class ConvertListToSet {
public static void main(String[] args) {
// Create a list with duplicate elements
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("apple");
list.add("orange");
// Convert list to HashSet
Set<String> hashSet = new HashSet<>(list);
System.out.println("HashSet: " + hashSet);
// Convert list to TreeSet
Set<String> treeSet = new TreeSet<>(list);
System.out.println("TreeSet: " + treeSet);
// Convert list to LinkedHashSet
Set<String> linkedHashSet = new LinkedHashSet<>(list);
System.out.println("LinkedHashSet: " + linkedHashSet);
}
}
5. Conclusion
In this tutorial, we've learned how to convert a List to a Set in Java using three different implementations of the Set interface: HashSet, TreeSet, and LinkedHashSet. Each implementation serves different purposes based on whether you need unordered, naturally ordered, or insertion-ordered collections. This operation is useful for removing duplicates and leveraging the unique properties of Sets in Java.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment