You can use retainAll method:

columnsOld.retainAll (columnsNew);
Answer from Roman on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java list › intersection of two lists in java
Intersection of Two Lists in Java | Baeldung
April 4, 2025 - The intersection should contain each common element only once. The order shouldn’t matter, thus toSet is the most straightforward choice, but we can also use toList or another collector method. For more details, review our guide to Java 8’s Collectors. What if our Lists don’t contain Strings but rather instances of a custom class we’ve created?
Discussions

intersection for multiple lists - Java Programming
I need to get the intersection of multiple lists and was wondering if there is a more straight forward way than creating a class that organises the intersection for added elements from different lists... More on forums.oracle.com
🌐 forums.oracle.com
August 7, 2006
Finding the XOR between multiple lists of no overlapping time intervals (Java)
What exactly do you mean by XOR between 2 lists of time intevals? Do you mean a merge or a union between those 2 lists? What you can do is simply explain, what you want to achieve with those 2 lists of time intervals. More on reddit.com
🌐 r/learnprogramming
4
1
October 15, 2024
sets - Is there a simple way to perform the opposite of intersection on two lists? - Mathematica Stack Exchange
Mathematica is part of Stack Overflow’s open communities: specialist spaces where curiosity is welcome, knowledge is shared freely, and the best answers rise to the top · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on mathematica.stackexchange.com
🌐 mathematica.stackexchange.com
Intersection of more than two sets

One (slightly annoying) approach is to do the same thing as implementations of those functions, which for HashSet can be done like

set1.iter().filter(|k| set2.contains(k)).filter(|k| set3.contains(3))...

Or, like,

let sets = [&set2, &set3, ...];
set1.iter().filter(|k| sets.all(|s| s.contains(k)))

(This could probably be optimised by choosing set1 to be the one with the fewest elements.)

The BTreeMap version is a little more complicated: uses the normal iters and walks over them at the same time, using the fact that they're in sorted order to be more efficient than doing a log n contains check for each element (similar to the merge step of a merge sort). I'm not sure of a pithy way like the filter above to implement this.

More on reddit.com
🌐 r/rust
22
22
February 20, 2017
🌐
Medium
medium.com › @alxkm › how-to-find-intersection-and-difference-between-two-sets-or-lists-in-java-5a873a8bff5d
Java Interview: How to Find Intersection and Difference Between Two Sets or Lists in Java | by Alex Klimenko | Medium
August 9, 2025 - Set<String> set1 = new HashSet<>(list1); Set<String> set2 = new HashSet<>(list2); set1.retainAll(set2); // Keeps only common elements System.out.println("Intersection: " + set1); Set<String> diff = new HashSet<>(list1); diff.removeAll(list2); // Removes common elements System.out.println("Difference (list1 - list2): " + diff);
🌐
Java Guides
javaguides.net › 2024 › 05 › java-find-intersection-of-two-lists.html
Java: Find Intersection of Two Lists
May 29, 2024 - Two lists are created and populated with elements. The retainAll method is used to retain only the elements in list1 that are also in list2. ... The Stream API (introduced in Java 8) provides a modern and concise way to find the intersection of two lists.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-for-finding-intersection-point-of-two-linked-lists
Java Program For Finding Intersection Point Of Two Linked Lists - GeeksforGeeks
July 23, 2025 - Then we can traverse both the lists in parallel till we come across a common node. (Note that getting a common node is done by comparing the address of the nodes) ... // Java program to get intersection point // of two linked list class LinkedList { static Node head1, head2; static class Node { int data; Node next; Node(int d) { data = d; next = null; } } /* Function to get the intersection point of two linked lists head1 and head2 */ int getNode() { int c1 = getCount(head1); int c2 = getCount(head2); int d; if (c1 > c2) { d = c1 - c2; return _getIntesectionNode(d, head1, head2); } else { d =
🌐
Baeldung
baeldung.com › home › algorithms › efficient ways to find the intersection of lists
Efficient Ways to Find the Intersection of Lists | Baeldung on Computer Science
March 18, 2024 - To calculate the intersection of multiple lists, we need to determine the mutual elements between them. In the example above, the mutual elements are marked with a red color. One thing to note here is the case of a value being repeated more ...
Find elsewhere
🌐
LeetCode
leetcode.com › problems › intersection-of-two-linked-lists
Intersection of Two Linked Lists - LeetCode
Intersection of Two Linked Lists - Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.
🌐
Oracle
forums.oracle.com › ords › apexds › post › intersection-for-multiple-lists-0215
intersection for multiple lists - Java Programming
August 7, 2006 - I need to get the intersection of multiple lists and was wondering if there is a more straight forward way than creating a class that organises the intersection for added elements from different lists...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-find-the-intersection-between-two-collection
Java Program to Find the Intersection Between Two Collection - GeeksforGeeks
July 23, 2025 - In this article we will use two collection framework classes vector class and ArrayList class to find the intersection between the two collection. ... Store the elements in the First collection and in the second collection (Array List). Now Iterate the First collection and checks whether the second collection contains elements of the first collection or not. If not contains remove the element from the first collection. Print the first collection. ... // Java Program to Remove All the Elements from the First // Collection if it is not Available in the Second // Using ArrayList.contains() Method
🌐
AlgoCademy
algocademy.com › link
Intersection Of Two Linked Lists in Java | AlgoCademy
Traverse both lists simultaneously until we find the intersection node or reach the end of the lists. A naive solution would involve using nested loops to compare each node of the first list with each node of the second list.
🌐
Reddit
reddit.com › r/learnprogramming › finding the xor between multiple lists of no overlapping time intervals (java)
r/learnprogramming on Reddit: Finding the XOR between multiple lists of no overlapping time intervals (Java)
October 15, 2024 -

Does any one have any elegant solution for finding the XOR between two lists of time intervals, preferably in Java? You can assume that, within each list, the time intervals are non-overlapping, but it is not safe to assume that between the two lists they are non-overlapping. I’ve been struggling with it the past couple of days.

🌐
Medium
medium.com › @shaswata.ssaha › efficiently-finding-the-intersection-of-two-arrays-in-java-ce8b6c08c0ec
Efficiently Finding the Intersection of Two Arrays in Java | by Shaswata Saha | Medium
July 2, 2024 - To solve this problem, we need to find common elements between two arrays and ensure that each common element appears in the result as many times as it appears in both input arrays. A HashMap can help efficiently count occurrences and find intersections. Count Occurrences: Use a HashMap to count how many times each element appears in nums1. Find Intersection: Iterate over nums2 and check if the element exists in the HashMap with a count greater than zero. If it does, add it to the result list and decrement the count in the map.
🌐
takeuforward
takeuforward.org › data-structure › find-intersection-of-two-linked-lists
Find intersection of Two Linked Lists - Tutorial
class Node: def __init__(self, val): self.num = val self.next = None # Utility function to insert node at the end of the linked list def insertNode(head, val): newNode = Node(val) if not head: head = newNode return head temp = head while temp.next: temp = temp.next temp.next = newNode return head # Utility function to get the difference in lengths of two linked lists def getDifference(head1, head2): len1, len2 = 0, 0 while head1 or head2: if head1: len1 += 1 head1 = head1.next if head2: len2 += 1 head2 = head2.next return len1 - len2 # If negative, length of list2 > length of list1, else vice-
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › intersection-of-two-sorted-linked-lists
Intersection of two Sorted Linked Lists - GeeksforGeeks
The idea is to maintain two pointers, one for each list. At each step, compare their values: If both values are equal, add the value to the result list and move both pointers. If the value in the first list is smaller, move the pointer of the ...
Published: July 23, 2025
🌐
ThisCodeWorks
thiscodeworks.com › union-and-intersection-of-two-arraylists-java › 62ea3573a0e8080015c7c31f
Union and intersection of two ArrayLists | thiscodeWorks
import java.util.*; public class UnionIntersectionOfTwoListsExample { public static void main (String[] args) { List<String> list1 = new ArrayList<String>(Arrays.asList("a","b","c","d","e")); List<String> list2 = new ArrayList<String>(Arrays.asList("b","d","f","g")); //Find union of two lists System.out.println("Union of List1 and List2 :" + getUnionOfLists(list1, list2)); //Find intersect of lists using Stream API (Java 8) System.out.println("Intersection of List1 & List2 Method 1: " + getIntersectOfLists1(list1, list2)); //Find intersect of lists using retainAll() method System.out.println("