How to create a hash from two equal-length arrays (keys and values) in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

' ------------------------------------------------------------
' Class representing a key/value pair
' ------------------------------------------------------------
Public Class Pair
    Public Property Key As String
    Public Property Value As Integer

    Public Sub New(k As String, v As Integer)
        Key = k
        Value = v
    End Sub
End Class

Module Program

    ' ------------------------------------------------------------
    ' Build a List(Of Pair) (preserves original order)
    ' ------------------------------------------------------------
    Function MakePairList(keys As String(), values As Integer()) As List(Of Pair)
        Dim result As New List(Of Pair)

        For i As Integer = 0 To keys.Length - 1
            result.Add(New Pair(keys(i), values(i)))
        Next

        Return result
    End Function

    ' ------------------------------------------------------------
    ' Build a sorted List(Of Pair) (sorted by key)
    ' ------------------------------------------------------------
    Function MakeSortedPairs(keys As String(), values As Integer()) As List(Of Pair)
        Dim arr = MakePairList(keys, values)

        arr.Sort(Function(a, b) String.Compare(a.Key, b.Key, StringComparison.Ordinal))

        Return arr
    End Function

    ' ------------------------------------------------------------
    ' Print parallel arrays
    ' ------------------------------------------------------------
    Sub PrintParallelArrays(keys As String(), values As Integer())
        For i As Integer = 0 To keys.Length - 1
            Console.WriteLine($"  {keys(i)} => {values(i)}")
        Next
    End Sub

    ' ------------------------------------------------------------
    ' Print List(Of Pair)
    ' ------------------------------------------------------------
    Sub PrintPairs(list As List(Of Pair))
        For Each p In list
            Console.WriteLine($"  {p.Key} => {p.Value}")
        Next
    End Sub

    ' ------------------------------------------------------------
    ' Main program demonstrating all three
    ' ------------------------------------------------------------
    Sub Main()
        Dim keys() As String = {"a", "b", "c"}
        Dim values() As Integer = {1, 2, 3}

        Dim ordered = MakePairList(keys, values)
        Dim sorted = MakeSortedPairs(keys, values)

        Console.WriteLine("Parallel arrays:")
        PrintParallelArrays(keys, values)

        Console.WriteLine()
        Console.WriteLine("List(Of Pair) (preserves order):")
        PrintPairs(ordered)

        Console.WriteLine()
        Console.WriteLine("Sorted List(Of Pair) (sorted by key):")
        PrintPairs(sorted)
    End Sub

End Module


' ------------------------------------------------------------
' run:
'
' Parallel arrays:
'   a => 1
'   b => 2
'   c => 3
'
' List(Of Pair) (preserves order):
'   a => 1
'   b => 2
'   c => 3
'
' Sorted List(Of Pair) (sorted by key):
'   a => 1
'   b => 2
'   c => 3
' ------------------------------------------------------------

 



answered 4 hours ago by avibootz
...