aspose file tools*
The moose likes Beginning Java and the fly likes Using HashMap on a text string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using HashMap on a text string" Watch "Using HashMap on a text string" New topic
Author

Using HashMap on a text string

Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
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
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using HashMap on a text string