VB.NET:检查列表项是否相等且计数相同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17787538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET: Check if List items are equal and have same count
提问by jor
How can I detect whether the items of two given lists are equal?
如何检测两个给定列表的项目是否相等?
Dim list1 As New List(Of Integer)
list1.AddRange({1, 2, 3})
Dim list2 As New List(Of Integer)
list2.AddRange({3, 2, 1})
If I compare them using SequenceEqualI get Falsebecause the order of the items is not the same. How can I compare them without sortingthem first, though?
如果我使用SequenceEqualI get比较它们,False因为项目的顺序不一样。但是,我如何在不先对它们进行排序的情况下比较它们?
EDIT: Please take into account that this should respect duplicates, for example {1, 2, 3, 1}is not the same as {1, 2, 3}(item 1occurs two times in the first list).
编辑:请考虑到这应该尊重重复项,例如{1, 2, 3, 1}与{1, 2, 3}(项目1在第一个列表中出现两次)不同。
采纳答案by jor
<System.Runtime.CompilerServices.Extension()> _
Function AreItemsEqual(Of T)(col1 As IEnumerable(Of T), col2 As IEnumerable(Of T)) As Boolean
' performance checks
If col1 Is col2 Then Return True
If col1 Is Nothing OrElse col2 Is Nothing Then Return False
If col1.Count <> col2.Count Then Return False
' compare their elements
Dim o1 As IEnumerable(Of T) = col1.OrderBy(Function(i) i)
Dim o2 As IEnumerable(Of T) = col2.OrderBy(Function(i) i)
Return o1.SequenceEqual(o2)
End Function
Usage:
用法:
If list1.AreItemsEqual(list2) Then
...
回答by Tim Schmelter
If you want to know if both lists contain the same items you can use Enumerable.Except:
如果您想知道两个列表是否包含相同的项目,您可以使用Enumerable.Except:
Dim bothContainSameItems As Boolean
If list1.Count > list2.Count Then
bothContainSameItems = Not list1.Except(list2).Any()
Else
bothContainSameItems = Not list2.Except(list1).Any()
End If
or, with the help of HashSet(Of T):
或者,在以下人员的帮助下HashSet(Of T):
Dim l1Set = New HashSet(Of Integer)(list1)
Dim l2Set = New HashSet(Of Integer)(list2)
bothContainSameItems = l1Set.SetEquals(l2Set)
Note that both approaches will ignore duplicates. So they will return equalfor:
请注意,这两种方法都会忽略重复项。所以他们将返回equal:
list1.AddRange({1, 1, 2, 3})
list2.AddRange({3, 2, 1, 3})
Here's a possible way to also check if all numbers have the same count in both lists:
这是检查两个列表中所有数字是否具有相同计数的一种可能方法:
bothContainSameItems = list1.Count = list2.Count
If bothContainSameItems Then
Dim l1Ordered = list1.OrderBy(Function(i) i).ToList()
Dim l2Ordered = list2.OrderBy(Function(i) i).ToList()
For i As Int32 = 0 To l1Ordered.Count - 1
If l1Ordered(i) <> l2Ordered(i) Then
bothContainSameItems = False
Exit For
End If
Next
End If
回答by Steve
Also working with
还与
Dim list1 As New List(Of Integer)
list1.AddRange({1, 2, 3})
Dim list2 As New List(Of Integer)
list2.AddRange({3, 2, 1})
Dim list3 = list1.Union(list2)
if list3.OrderBy(Function(i) i).SequenceEqual(list1.OrderBy(Function(i) i)) then
Console.WriteLine("Equal")
else
Console.WriteLine("Not Equal")
end if
Return Value: An IEnumerable(Of T) that contains the elements from both input sequences, excluding duplicates.
返回值:一个 IEnumerable(Of T),包含来自两个输入序列的元素,不包括重复项。

