🎓 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
retainAll method, and the Stream API (Java 8 and later).Table of Contents
- Introduction
- Using Loops
- Using
retainAllMethod - Using
StreamAPI - Conclusion
Introduction
In Java, lists are dynamic data structures that store elements of the same type. Finding the intersection of two lists means identifying the elements that are common to both lists. This can be useful in various scenarios, such as filtering data or finding commonalities between data sets.
Using Loops
One way to find the intersection of two lists is by iterating through each list and collecting the common elements.
Example
import java.util.ArrayList;
import java.util.List;
public class ListIntersectionExample {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);
List<Integer> list2 = new ArrayList<>();
list2.add(3);
list2.add(4);
list2.add(5);
list2.add(6);
list2.add(7);
List<Integer> intersection = findIntersection(list1, list2);
System.out.println("Intersection: " + intersection);
}
public static List<Integer> findIntersection(List<Integer> list1, List<Integer> list2) {
List<Integer> intersection = new ArrayList<>();
for (Integer element : list1) {
if (list2.contains(element)) {
intersection.add(element);
}
}
return intersection;
}
}
Explanation
- Two lists are created and populated with elements.
- A loop is used to iterate through the first list.
- If an element from the first list is found in the second list, it is added to the intersection list.
Output:
Intersection: [3, 4, 5]
Using retainAll Method
The retainAll method provided by the List interface is a convenient way to find the intersection of two lists.
Example
import java.util.ArrayList;
import java.util.List;
public class ListIntersectionExample {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);
List<Integer> list2 = new ArrayList<>();
list2.add(3);
list2.add(4);
list2.add(5);
list2.add(6);
list2.add(7);
list1.retainAll(list2);
System.out.println("Intersection: " + list1);
}
}
Explanation
- Two lists are created and populated with elements.
- The
retainAllmethod is used to retain only the elements inlist1that are also inlist2.
Output:
Intersection: [3, 4, 5]
Using Stream API
The Stream API (introduced in Java 8) provides a modern and concise way to find the intersection of two lists.
Example
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ListIntersectionExample {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);
List<Integer> list2 = new ArrayList<>();
list2.add(3);
list2.add(4);
list2.add(5);
list2.add(6);
list2.add(7);
List<Integer> intersection = list1.stream()
.filter(list2::contains)
.collect(Collectors.toList());
System.out.println("Intersection: " + intersection);
}
}
Explanation
- Two lists are created and populated with elements.
- A stream is created from
list1. - The
filtermethod is used to keep only the elements that are present inlist2. - The
collectmethod gathers the filtered elements into a new list.
Output:
Intersection: [3, 4, 5]
Conclusion
Finding the intersection of two lists in Java can be accomplished using various methods, each with its own advantages. Using loops provides a clear and straightforward approach, suitable for any type of list. The retainAll method offers a concise and direct way to achieve the same result with less code. The Stream API provides a modern and functional programming approach, making the code more readable and expressive. Depending on your specific use case and preferences, you can choose the method that best fits your needs.
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