Lookup(TKey, TElement) Represents a collection of keys each mapped to one or more values. : HashSet « Data Structure « VB.Net






Lookup(TKey, TElement) Represents a collection of keys each mapped to one or more values.

 


Structure Product
    Public Company As String
    Public Weight As Double
    Public TrackingNumber As Long
End Structure

Public Class Example
    Public Shared Sub Main()
    Dim packages As New System.Collections.Generic.List(Of Product)(New Product() _
        {New Product With {.Company = "A", .Weight = 25.2, .TrackingNumber = 1L}, _
          New Product With {.Company = "B", .Weight = 18.7, .TrackingNumber = 2L}, _
          New Product With {.Company = "C", .Weight = 6.0, .TrackingNumber = 3L}, _
          New Product With {.Company = "D", .Weight = 9.3, .TrackingNumber = 4L}, _
          New Product With {.Company = "E", .Weight = 33.8, .TrackingNumber = 5L}})

    Dim lookup As ILookup(Of Char, String) = packages.ToLookup(Function(ByVal p) Convert.ToChar(p.Company.Substring(0, 1)), _
                          Function(ByVal p) p.Company & " " & p.TrackingNumber)

    For Each packageGroup As IGrouping(Of Char, String) In lookup
        Console.WriteLine(packageGroup.Key)
        For Each str As String In packageGroup
            Console.WriteLine(str)
        Next
    Next
    Dim count As Integer = lookup.Count
    Dim cgroup As System.Collections.Generic.IEnumerable(Of String) = lookup("C"c)
    For Each str As String In cgroup
        Console.WriteLine(str)
    Next
    Dim hasG As Boolean = lookup.Contains("G"c)
 End Sub
End Class

   
  








Related examples in the same category

1.KeyedCollection(TKey, TItem) is the abstract base class for a collection whose keys are embedded in the values.
2.ReadOnlyCollection(T) Class Provides the base class for a generic read-only collection.
3.HashSet(T) Class Represents a set of values.
4.Create a HashSet from another HashSet
5.Create HashSet(Of T) class that uses the default equality comparer for the set type
6.Create HashSet(Of T) class from another collection
7.Remove element from HashSet with condition function
8.HashSet(T).Clear Method removes all elements from a HashSet(Of T) object.
9.HashSet(T).Contains Method tells whether a HashSet(Of T) object contains the specified element.
10.HashSet(T).ExceptWith Method removes all elements in the specified collection from the current HashSet(Of T) object.
11.HashSet(T).IsProperSubsetOf Method tells whether a HashSet(Of T) object is a proper subset of the specified collection.
12.HashSet(T).SymmetricExceptWith Method keeps elements that are present either in that object or in the specified collection, but not both.