Let's say i have a nested List which contains n^2 element in it and also i have a HashMap which contains n^2 keys in it. I want to get common elements between the list and hashmap with using retainAll()
function. What is the time complexity of retainAll() in this case?
List<List<Integer> list = new ArrayList<>();
Map<List<Integer>, Integer> hashmap = new HashMap<List<Integer>, Integer>();
list.retainAll(hashmap);
To get the commons,i'm using this loop but it has n^2 complexity.
List<List<Integer>> commons = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (hashmap.get(list.get(i)) != null) {
commons.add(list.get(i));
}
}
Also if you have an algorithm which has better time complexity to get intersection of them, i need it.
EDIT : Actually the problem is , i have a list of integers size of n . But i divided that list into sublists and sublists count became n^2. Since i want to find out that hashmap contains every sublist as a key in it, i used n^2 in question. But it's too big according to my whole project. I'm searching complexity decreasing ways.