检查列表<StringCollection>是否包含列表<string>
Check if a list<StringCollection> contains list<string>
给定 list<StringCollection>
如何最有效地检查所有 string
是否包含在任何 StringCollection
中?
示例:
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// Create and initializes a new StringCollection.
StringCollection myCol0 = new StringCollection();
StringCollection myCol1 = new StringCollection();
StringCollection myCol2 = new StringCollection();
StringCollection SearchCol = new StringCollection();
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr0 = new String[] { "RED", "car", "boat" };
myCol0.AddRange( myArr0 );
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr1 = new String[] { "Blue", "Goku", "Nappa" };
myCol1.AddRange( myArr1 );
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr2 = new String[] { "Yellow", "Winter", "Summer" };
myCol2.AddRange( myArr2 );
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr3 = new String[] { "Yellow", "Blue", "RED" };
SearchCol.AddRange( myArr3 );
List<StringCollection> a = new List<StringCollection>();
a.Add(myCol0);
a.Add(myCol1);
a.Add(myCol2);
}
}
在这种情况下,我想知道 SearchCol
中的字符串是否包含在存储在 List<StringCollection> a
中的字符串集合中
在这种情况下,我只想知道 List<StringCollection> a
中未包含哪些 searchCol 字符串
我认为唯一可行的方法是通过双 for 循环?
有没有比字符串集合更有效的数据结构?
Is there any datastructure that would be more efficient rather than
an stringcollection
高效的方式是什么?当然,您通常应该使用 IEnumerable<string>
(如 string[]
或 List<string>
),因为 StringCollection
不是通用的。
但你也可以使用StringCollection
,你必须将每个项目从对象转换为字符串:
var allStrings = a.SelectMany(c => c.Cast<string>());
var searchStrings = SearchCol.Cast<string>();
bool allSearchStringsAreContained = searchStrings.All(allStrings.Contains);
至于“如何做最有效”,这种简单的方法是有效的,但如果您有大量要搜索的字符串列表或庞大的字符串列表,您可以使用基于集合的方法:
HashSet<string> set = new HashSet<string>(searchStrings);
bool allSearchStringsAreContained = set.IsSubsetOf(allStrings);
最后,如果您想忽略大小写,请将“RED”和“Red”视为相同:
方法一:
bool allSearchStringsAreContained = searchStrings.All(s => allStrings.Contains(s, StringComparer.OrdinalIgnoreCase));
方法二:
HashSet<string> set = new HashSet<string>(searchStrings, StringComparer.OrdinalIgnoreCase);
给定 list<StringCollection>
如何最有效地检查所有 string
是否包含在任何 StringCollection
中?
示例:
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// Create and initializes a new StringCollection.
StringCollection myCol0 = new StringCollection();
StringCollection myCol1 = new StringCollection();
StringCollection myCol2 = new StringCollection();
StringCollection SearchCol = new StringCollection();
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr0 = new String[] { "RED", "car", "boat" };
myCol0.AddRange( myArr0 );
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr1 = new String[] { "Blue", "Goku", "Nappa" };
myCol1.AddRange( myArr1 );
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr2 = new String[] { "Yellow", "Winter", "Summer" };
myCol2.AddRange( myArr2 );
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr3 = new String[] { "Yellow", "Blue", "RED" };
SearchCol.AddRange( myArr3 );
List<StringCollection> a = new List<StringCollection>();
a.Add(myCol0);
a.Add(myCol1);
a.Add(myCol2);
}
}
在这种情况下,我想知道 SearchCol
中的字符串是否包含在存储在 List<StringCollection> a
在这种情况下,我只想知道 List<StringCollection> a
我认为唯一可行的方法是通过双 for 循环? 有没有比字符串集合更有效的数据结构?
Is there any datastructure that would be more efficient rather than an stringcollection
高效的方式是什么?当然,您通常应该使用 IEnumerable<string>
(如 string[]
或 List<string>
),因为 StringCollection
不是通用的。
但你也可以使用StringCollection
,你必须将每个项目从对象转换为字符串:
var allStrings = a.SelectMany(c => c.Cast<string>());
var searchStrings = SearchCol.Cast<string>();
bool allSearchStringsAreContained = searchStrings.All(allStrings.Contains);
至于“如何做最有效”,这种简单的方法是有效的,但如果您有大量要搜索的字符串列表或庞大的字符串列表,您可以使用基于集合的方法:
HashSet<string> set = new HashSet<string>(searchStrings);
bool allSearchStringsAreContained = set.IsSubsetOf(allStrings);
最后,如果您想忽略大小写,请将“RED”和“Red”视为相同:
方法一:
bool allSearchStringsAreContained = searchStrings.All(s => allStrings.Contains(s, StringComparer.OrdinalIgnoreCase));
方法二:
HashSet<string> set = new HashSet<string>(searchStrings, StringComparer.OrdinalIgnoreCase);