## Intersection between two arraylist Java

java intersection of two arrays

java compare two lists of objects

get uncommon elements from two lists java 8

intersection operation in java

java 8 compare two lists and find differences

java stream compare two lists

intersect method java

I wanted to find an intersection between two arraylist that are of byte[] format and return the common indices. I have the code as follows and it works correctly:

ArrayList<Integer> intersect = new ArrayList<Integer>(); for(int j = 0; j < A.size(); j++) { byte [] t = A.get(j); for (int j1 = 0; j1 < B.size(); j1++) { byte[] t1 = B.get(j1); if (Arrays.equals(t, t1)) { intersect.add(j); break; } } }

However, as you can see, I have to use two for loops. Is there any way I can do it without using any for loops? I have tried to use **"retainAll"** but for some reason, it kept giving me an empty array. What can be the possible reason for that?

If you just dont want to use for loops, you may try the Java 8 streams, since you have not shared the complete code I did not try it out.

List<T> intersect = list1.stream() .filter(list2::contains) .collect(Collectors.toList());

**Intersection of Two Lists in Java,** Let's create two Lists of Strings with some intersection — both having some duplicated elements: ? 1. 2. List<String> list = Arrays.asList No, retainAll is not intersection for list. In above, all elements in col that are not in otherCol are removed. Let's say otherCol is {a,b,b,c} and col is {b,b,b,c,d}. Then col ends up with {b,b,b,c} which is not the strictly the intersection of the two.

I think for `T`

your solution with brutforce is not so bad. This is a small refactoring of it. Yes, for specific `T`

(e.g. `int`

) you could use special structures, but in general case, I think this is not implementable.

public static <T> List<Integer> intersect(List<T> A, List<T> B, BiPredicate<T, T> isEqual) { List<Integer> intersect = new LinkedList<>(); int i = -1; for (T a : A) { i++; if (B.stream().anyMatch(b -> isEqual.test(a, b))) intersect.add(i); } return intersect; }

Client code could look like:

List<byte[]> a = Collections.emptyList(); List<byte[]> b = Collections.emptyList(); List<Integer> intersect = intersect(a, b, Arrays::equals);

**Union and Intersection of ArrayList in Java – Madhur Ahuja,** Intersection. import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) To find the intersection of two arrays in java use two loops. The outer loop is to iterate the elements of the first array whereas, the second loop is to iterate the elements of the second array. Within the second loop compare the elements of the two arrays: Example. Live Demo

This may help:

public <T> List<T> intersection(List<T> list1, List<T> list2) { List<T> list = new ArrayList<T>(); for (T t : list1) { if(list2.contains(t)) { list.add(t); } } return list; }

Reference: Intersection and union of ArrayLists in Java

**Find the intersection of ArrayList elements inside an ArrayList,** But how do I make it work for multiple ArrayLists inside one ArrayList ? I was asked this in an interview. Worked for separate lists, but when the Java 8 : Find union and intersection of two Lists (ArrayLists) February 17, 2020 February 20, 2020 Editorial Staff This tutorial shows you how to find union and intersection of two Lists (ArrayLists).

**How to get intersection between two arrays,** Learn to find the intersection between two arrays in Java using HashSet class. An intersection is a group of common items that belong to two different sets. In this quick article, we've seen how to use streams to calculate the intersection of two lists. There are many other operations that used to be quite tedious but are pretty straightforward if we know our way around the Java Stream API. Take a look at our further tutorials with Java streams here. Code examples are available over on GitHub.

**How to get union between two arrays,** Learn to find the union between two arrays in Java using HashSet class. In set theory, the union (denoted by U) of a collection of sets is the set of all elements in Union and Intersection of ArrayList in Java. 29 July 2014. Tweet. Buffer. Intersection import java.util.ArrayList; import java.util.Arrays; import java.util.List

**Find common elements in two ArrayLists in Java,** The List 1 now contains the common elements only. Below is the implementation of the above approach: Program: By modifying the contents of List1. filter_none. Learn to compare two arraylists in Java with simple examples. We will first test if two arraylists are equal or not. If both lists are not equal, we will find the difference between lists. The difference in list is equals to another third list which contains either additional elements or missing elements. Also learn to find common elements

##### Comments

*I have tried to use "retainAll" but for some reason, it kept giving me an empty array. What can be the possible reason for that?*Because the implementation of`equals()`

for arrays checks for identity, not contents.- stackoverflow.com/questions/5283047/… might provide some useful information using other collection types to get the same behavior.
- @Shahnewaz Given that you need control over the equality check, the code you are using is probably the best you can do without another data structure. Also, given that you are trying to "retain" the position of the array in the list (and not the array itself),
`retainsAll()`

would never have given you the result you were looking for. - Possible duplicate of Intersection and union of ArrayLists in Java
- @Chisko eh... not
*really*. The concept is the same, but the accepted answer is not going to work. - So, for my list it should be List<T> intersect = A.stream().filter(B::contains).collect(Collectors.toList()); Right?
- @SeanBright Why on earth it
work for*should*`<T> = Array`

if the OP is not even asking for that?!!! - @SeanBright You are right... OP's question is very bad!
- you sneaky punk ;)