我有两个哈希集合,就像这样:
HashSet<string> log1 = new HashSet<string>(File.ReadLines("log1.txt"));
HashSet<string> log2 = searcher(term);
我该如何比较这两个东西?
我想确保log2
中不包含任何来自log1
的条目。换句话说,我想要移除log2
中所有(如果有的话)log1
已经存在的项目。
我有两个哈希集合,就像这样:
HashSet<string> log1 = new HashSet<string>(File.ReadLines("log1.txt"));
HashSet<string> log2 = searcher(term);
我该如何比较这两个东西?
我想确保log2
中不包含任何来自log1
的条目。换句话说,我想要移除log2
中所有(如果有的话)log1
已经存在的项目。
你可以使用 HashSet<T>.ExceptWith 方法 从 log2
中删除所有与 log1
相同的项:
log2.ExceptWith(log1);
或者,你可以使用Enumerable.Except扩展方法创建一个新的HashSet<T>,而不修改两个原始的集合:
HashSet<string> log3 = new HashSet<string>(log2.Except(log1));
ExceptWith
函数吗?
从当前 HashSet 对象中移除指定集合中的所有元素。