Marlo Kuisma
Marlo Kuisma

Reputation: 101

Java ArrayList<ArrayList<>> indexOf regardless of the order

I have created an ArrayList containing ArrayLists in Java.

ArrayList<ArrayList<Integer>> arrays = new ArrayList<ArrayList<Integer>>();

Now I'm trying to get the index of a list containing a specific combination of items, regardless of order, using the arrays.indexOf() method.

How can I do this the easiest and fastest way? The array could possibly be very long, so I can't try every possible order.

Upvotes: -1

Views: 94

Answers (2)

Robin Krahl
Robin Krahl

Reputation: 5308

Either use a Set (if there are no duplicates), or sort the stored lists as well as the list you want to lookup. If the previous order is important, maintain a second list with the original data.

Upvotes: 2

Eran
Eran

Reputation: 394016

in other words I only want it to contain the same items

This means that a Set is more appropriate than a List, since when you compare two Sets, you are checking if they contain the same items. There is no meaning to order.

ArrayList<Set<Integer>> arrays = new ArrayList<Set<Integer>>();

Upvotes: 2

Related Questions