• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using HashMap on a text string

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

Wondering what would be the best way to store a text string delimited by some sort of separator (e.g. ',') where the first element of the string would be the key and rest would be the value?
For example I would like to have a text string like -

'0001, abc, cde, xyz, 11/30/05' stored in a hashMap where the key would be 0001 and the value would be - 'abc, cde, xyz, 11/30/05'.

Later I would be comparing other strings with matching values of the first element. I guess using HashMap would be an efficient way to do this comparison?

Any help would be much appreciated.

Thanks
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about using the String.split method. You can use Regex to get the first comma as the split point, then you have two Strings in the returned String array, get the 0 element as the Key and the 1 element as the value.

Mark
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark, how do you do that so you get two parts instead of 5? You don't want to split on all the commas in the string, just the first one.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
Mark, how do you do that so you get two parts instead of 5? You don't want to split on all the commas in the string, just the first one.



The String.split() method is actually overloaded with a version that allows you to set a limit. Just set the limit to 2.

Henry
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic