Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在List和HashMap之间使用retainAll()函数的时间复杂度是多少?_Java_Algorithm_Arraylist_Hashmap_Big O - Fatal编程技术网

Java 在List和HashMap之间使用retainAll()函数的时间复杂度是多少?

Java 在List和HashMap之间使用retainAll()函数的时间复杂度是多少?,java,algorithm,arraylist,hashmap,big-o,Java,Algorithm,Arraylist,Hashmap,Big O,假设我有一个嵌套列表,其中包含n^2个元素,还有一个HashMap,其中包含n^2个键。我想使用retainal()函数获得列表和hashmap之间的公共元素。在这种情况下,retainal()的时间复杂度是多少 List<List<Integer> list = new ArrayList<>(); Map<List<Integer>, Integer> hashmap = new HashMap<List<Integer>

假设我有一个嵌套列表,其中包含n^2个元素,还有一个HashMap,其中包含n^2个键。我想使用
retainal()
函数获得列表和hashmap之间的公共元素。在这种情况下,retainal()的时间复杂度是多少

List<List<Integer> list = new ArrayList<>();
Map<List<Integer>, Integer> hashmap = new HashMap<List<Integer>, Integer>();
list.retainAll(hashmap);

List嗯,我的意思是,如果列表中有
n^2
项,那么复杂性就是
O(n^2)
,尽管这说起来很奇怪。通常<代码> N< /代码>是集合的大小,所以Reluth<<代码>的时间复杂度考虑“<代码> o(n)< /代码> .< 就我所能想到的,不可能有一个算法能为这个问题产生更好的时间复杂度。您必须至少迭代列表一次


您可以做的是切换数据结构。阅读更多内容:

看起来像是@GuilhermeSchaidhauerCastro的复制品,我在这篇文章之前检查过它,但我不能确定它是否相同。HashSet和HashMap可能会导致差异。我也不能清楚地得到答案。谢谢@Rubydesic。实际上问题是,我有一个n大小的整数列表。但我把列表分成了子列表,子列表的计数变成了n^2。因为我想知道hashmap包含每个子列表作为其中的一个键,所以我使用了n^2。但根据我的整个计划,它太大了。我在寻找降低复杂性的方法。
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));
        }
    }