Hashtable is often used to describe the set of index keys/value groups. The key values of hashtable are case sensitive.
Dim Ohash As New Hashtable ()
Dim Bisfind As Boolean
Ohash. Add ( " A " , " Valuea " )
Ohash. Add ( " B " , " Valuea " )
Ohash. Add ( " C " , " Valuea " )
Bisfind = Ohash. Contains ( " A " ) ' The specified value "A" does not exist.
Bisfind = Ohash. Contains ( " A " ) ' The specified value "A" exists.
What if we need to make hasktable case insensitive? One common practice is to convert all the key values into lowercase (or upper-case) when adding/removing a hasktable, and convert the key values into lowercase (or upper-case) when determining ).
Dim Ohash As New Hashtable ()
Dim Bisfind As Boolean
Ohash. Add ( " A " . Tolower, " Valuea " )
Ohash. Add ( " B " . Tolower, " Valuea " )
Ohash. Add ( " C " . Tolower, " Valuea " )
Bisfind = Ohash. Contains ( " A " . Tolower) ' The specified value "A" exists.
Bisfind = Ohash. Contains ( " A " . Tolower) ' The specified value "A" exists.
Of course, the above method is feasible, but it is a little troublesome. If the key value is not converted, it will easily lead to a situation that cannot be found. The best way is to handle it by hashtable itself, allows developers to ignore the case sensitivity of key values.
The syntax for constructing multiple loads in hasktable is as follows:
Public Sub new () Sub New(_
EqualitycomparerAsIequalitycomparer _
)
Therefore, we only need to write a category with the iequalitycomparer interface for key-value comparison.
Public Class thashkeycomparer Class Thashkeycomparer
Implements Iequalitycomparer
Public Function compute S1 () Function When S1 ( Byval X As Object , Byval Y As Object )_
As Boolean Implements Iequalitycomparer. equals
Return ( CSTR (X). tolower = CSTR (Y). tolower)
End Function
Public Function gethashcode1 () function gethashcode1 ( byval OBJ as Object ) _
as integer implements iequalitycomparer. gethashcode
return obj. tostring (). tolower (). gethashcode ()
end function
End Class
To use hasktable, you only need to use a custom iequalitycomparer class to initialize hasktable.
Dim Ohash As New Hashtable ( New Thashkeycomparer ())
Dim Bisfind As Boolean
Ohash. Add ( " A " , " Valuea " )
Ohash. Add ( " B " , " Valuea " )
Ohash. Add ( " C " , " Valuea " )
Bisfind = Ohash. Contains ( " A " ) ' The specified value "A" exists.
Bisfind = Ohash. Contains ( " A " ) ' The specified value "A" exists.