简体   繁体   English

在ArrayList中搜索

[英]Search in ArrayList

I have 2 ArrayLists that have a Array of Strings as a "component". 我有2个ArrayLists,它们有一个字符串数组作为“组件”。 I want to find the "components" whose first element is the same in both ArrayLists. 我想找到两个ArrayLists中第一个元素相同的“components”。 To be more clear: 更清楚:

ArrayList One
first component => {"0", "zero"}
second component => {"1", "one"}
ArrayList Two
first component => {"1", "uno"}
second component => {"2", "two"}

I would like to loop through ArrayList Two and find {"1","uno"}. 我想遍历ArrayList Two并找到{“1”,“uno”}。 So far I have a nested loop that loops through the first array and then checks the current component to each component in ArrayList Two. 到目前为止,我有一个嵌套循环,循环遍历第一个数组,然后检查当前组件到ArrayList Two中的每个组件。

    for(int i=0; i<One.size(); i++)
    {
        for(int j=0; j<Two.size(); j++)
        {
            if( fileOne.get(i)[0].equals( Two.get(j)[0] ) )
            {
                System.out.print( Two.get(j)[0]+" " );
                System.out.print( Two.get(j)[1] );
                System.out.println();
            }
        }
    }

I think there should be a better solution. 我认为应该有一个更好的解决方案。 Any help 任何帮助

You might try a hashmap. 您可以尝试使用hashmap。 Initialize it from the first ArrayList by mapping the first element of each component to the component (or the index of the component). 通过将每个组件的第一个元素映射到组件(或组件的索引),从第一个ArrayList初始化它。 Then for each component of the second ArrayList, you can look up by the first element of each component to find the matching component (or discover that there isn't one when it returns null ). 然后,对于第二个ArrayList的每个组件,您可以通过每个组件的第一个元素查找以查找匹配组件(或者在返回null时发现没有组件)。

Use a HashSet. 使用HashSet。

Set<String> set = new HashSet<String>()
for(int i=0; i<One.size(); i++) {
  set.add(fileOne.get(i)[0]);
}

for(int i=0; i<Two.size(); i++) {
  String component[] = Two.get(j)
  if(set.contains(component[0])) {
    System.out.print( component[0]+" " );
    System.out.print( component[1] );
    System.out.println();
   }
}

Note: A List would not work in this case, because lookups in Lists are O(N). 注意:列表在这种情况下不起作用,因为列表中的查找是O(N)。 Lookups in HashSets are O(1), so building the set (first loop) is O(N). HashSets中的查找是O(1),因此构建集合(第一个循环)是O(N)。 Then going through your second array is O(M) and each lookup is O(1). 然后通过你的第二个数组是O(M),每个查找是O(1)。

Overall, this becomes O(N) + ( O(M) * O(1) ) = O(N+M) 总的来说,这变成O(N)+(O(M)* O(1))= O(N + M)

Edit: for Ted's comments. 编辑:对于特德的评论。

Use two hashsets and the method retainAll such that: 使用两个哈希集和方法retainAll ,以便:

One.retainAll(Two)

Complexity wise is better - only O(n+m) against your O(n*m). 复杂性更好 - 只有O(n + m)对你的O(n * m)。 And in terms of readability and maintainability is also better. 而且在可读性和可维护性方面也更好。 Notice that retainAll will modify the hashset One if you don't want behavior make a third copy. 注意retainAll将修改HashSet的One ,如果你不想做的行为第三个副本。

(edited in response to Ted's comment) (编辑以回应泰德的评论)

Without initialization work such as sorting/hashing, or maintaining a sorted/hashed list, there is no better solution, an optimal solution is O(n*m) which yours is. 如果没有初始化工作(如排序/散列)或维护排序/散列列表,则没有更好的解决方案,最佳解决方案是O(n * m)。 That's the optimal solution because you have to compare every element to every other element. 这是最佳解决方案,因为您必须将每个元素与每个其他元素进行比较。

I should also mention that in certain scenarios, it may be wiser to keep the list sorted. 我还要提一下,在某些情况下,保持列表排序可能更明智。 Then instead of comparing every element you could use a binary search or something. 然后,不是比较每个元素,你可以使用二进制搜索或其他东西。 But without sorting your list first, yes you have to compare all elements. 但是如果没有先排序列表,是的,你必须比较所有元素。 I believe the best possible sorting time is O(nlgn). 我相信最好的分拣时间是O(nlgn)。 Then after that sort process you'd still have to search the sorted list for your element, but searching a sorted list can be faster than searching an unsorted one. 然后在排序过程之后,您仍然需要搜索元素的排序列表,但搜索排序列表可能比搜索未排序列表更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM