将两个对象列表与字符串列表进行比较并确保不重叠

时间:2017-02-15 03:51:24

标签: vb.net list compare

我有一个包含多个列表的对象。

Public Class aObject
    Public Property title
    Public Property aList As List(Of String)
    Public Property bList As List(Of String)
    Public Property cList As List(Of String)
    Public Property dList As List(Of String)
End Class

我有另一个存储所有aObjects的对象。

Public Class bObject
 Private _LocalCollection As List(Of aObject)
 Private _RemoteCollection As List(Of aObject)
End Class

aObject中的每个列表都是不同的设置。如果我添加一个新设置,我希望能够确保所有组合永远不会交叉。所以,如果我在aList中存储字母,在bList中存储数字,并且有一个{1,6,7}和{a,d,z}的对象,我不想添加另一个带有列表的设置{2,6,8 }和{a,f,g}。但是,我想添加列表{1,6,7}和{b,c,f}。所有四个列表都是一样的。我需要检查四者的组合。我可以使用递归算法并检查所有值,但我想知道是否还有其他方法。

我使用了以下建议的答案并实施了它:

Public Function checkDuplicates(ByVal strChampions As List(Of String), ByVal strSummonerSpells As List(Of String), ByVal strModes As List(Of String), ByVal strMaps As List(Of String)) As Boolean
    Dim booDuplicates As Boolean = False
    For Each setting In _LocalSettings
        Dim l1 = setting.champions.Intersect(strChampions)
        If l1.Count() > 0 Then
            Dim l2 = setting.summonerspells.Intersect(strSummonerSpells)
            If l2.Count() > 0 Then
                Dim l3 = setting.modes.Intersect(strModes)
                If l3.Count() > 0 Then
                    Dim l4 = setting.maps.Intersect(strMaps)
                    If l4.Count() > 0 Then
                        booDuplicates = booDuplicates Or True
' I am going to create the duplicate settings here to know where to overwrite.
                    End If
                End If
            End If
        End If
    Next
    Return booDuplicates
End Function

1 个答案:

答案 0 :(得分:1)

如果检查列表的交集以查看是否存在共同元素,该怎么办?

https://msdn.microsoft.com/en-us/library/bb460136(v=vs.110).aspx

  

两组A和B的交集被定义为包含A的所有元素的集合,这些元素也出现在B中,但没有其他元素。

Sub Main()
    Dim a As New List(Of String) From {"1", "6", "7"}
    Dim b As New List(Of String) From {"a", "d", "z"}
    Dim c As New List(Of String) From {"2", "6", "8"}

    Console.WriteLine(String.Format("a intersects b: {0}", a.Intersect(b).Count > 0))
    Console.WriteLine(String.Format("a intersects c: {0}", a.Intersect(c).Count > 0))
    Console.WriteLine(String.Format("b intersects c: {0}", b.Intersect(c).Count > 0))

    Console.ReadLine()
End Sub

输出:

a intersects b: False
a intersects c: True
b intersects c: False