Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Collections key comparison in case-insensitive

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
I am using HashMap for storing some key value pairs.
Then i will retrieve the value from the HashMap by passing the key.

Later i found that the Key comparison done is case-sensitive , but i need the comparison to be case-insensitive.

Is there any other collections with case-insensitive comparison for the key object.

Thanks
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Parameswaran Thangavel:
hi all
I am using HashMap for storing some key value pairs.
Then i will retrieve the value from the HashMap by passing the key.

Later i found that the Key comparison done is case-sensitive , but i need the comparison to be case-insensitive.

Is there any other collections with case-insensitive comparison for the key object.

Thanks



Is this key entered by some user? or you are just iterating over the collection of Map.Entry and getting each and every value.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Key is entered by the user. so there is possibiliyt the user can enter in different cases.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Above approach is useful only if you don't have more than one key stored in Map with the same name but with different case like "Parameswaran" and "PArameswaran".

Naseem
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use the String as the key, but some wrapper around the String that implements equals and hashCode in a case-insensitive way.
 
reply
    Bookmark Topic Watch Topic
  • New Topic