Make the hashtable key value case insensitive

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.