一一检查ArrayList of Strings是否包含50个不同的String

Check if ArrayList of Strings contains 50 different Strings one by one

如何检查字符串的ArrayList是否包含字符串数组中的50个不同字符串中的每一个,并对ArrayList中的每个相同字符串做某事?

您可以使用此函数来检查数组中的所有字符串是否也在 ArrayList 中。如果您想添加额外的逻辑,例如每次找到匹配项时执行 doSomething(),您应该能够轻松调整代码。

ArrayList myList; // let's assume its initialized and filled with Strings
String[] strArray; // let's assume its initialized and filled with Strings

//this function returns true if all Strings in the array are also in your arraylist

public boolean containsAll(myList, strArray){
  //iterate your String array
  for(int i = 0; i < strArray.length; i++){
    if(!myList.contains(strArray[i])){
      //String is not in arraylist, no need to check the rest of the Strings
      return false;
    }
  }
 return true; 
}

为什么不使用 LINQ?

    List<String> duplicates = YourList.GroupBy(x => x)
                         .Where(g => g.Count() > 1)
                         .Select(g => g.Key)
                         .ToList();

请注意,这将 return 在一个新的 List<string> 中所有重复项,因此如果您只想知道源列表中哪些项目重复,您可以将 Distinct 应用于结果序列或使用上面给出的解决方案