File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Creating collections from a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating collections from a String " Watch "Creating collections from a String " New topic

Creating collections from a String

nehad sharma

Joined: Apr 05, 2012
Posts: 2
I have a scenario for which I am looking for the most efficient approach.

I have key value pairs coming to the system in form of a long string
for e.g. K1,V1;K1,V2;K2,V3;K2V4;K3,V5
I need to display it in form of

K1: V1,V2
K2: V3,V4
K3: V5

Approach I am thinking:
1. create string tokens token 1 - K1,V1 token 2 - K1V2 etc, add them to a list KeyValuesRawList
2. create string tokens token 1 K1, token 2 K2 etc, add them to a list KeyList
4. create a map finalMap, lists valueList1,valueList2,valueList3
3. Iterate through KeyValuesRawList,
3.1 create tokens tempK and tempV
3.2 if (tempK = K1)
add tempV to valueList1
if(tempK = K2)
add tempV to valueList2
if(tempK = K3)
add tempV to valueList3
3.3 Add values from list KeyList as keys to finalMap
3.4 Add values from lists valueList1,valueList2,valueList3 as values to finalMap

Anayonkar Shivalkar

Joined: Dec 08, 2010
Posts: 1545

Hi nehad sharma,

Welcome to CodeRanch!

It is good to see that you've put some effort while asking the question.

Now, coming to your question, I feel a much easier approach is to have a HashMap with String as key and ArrayList<String> as value.

That way, your key will be K1 and value will be ArrayList containing V1 and V2.

I hope this helps.

Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

Apache Commons has a solution to this called the MultiMap. If you can, see if you can just use that instead of re-inventing things.

Otherwise, the idea should be to use a Map that maps the String key to a list of values (Map<String, List<String>>). You would add values into the Map like this:

As for how to get from a String where the values are listed like: K1,KV1;K1,V2;etc... into the Map, there probably isn't a need to go through all those intermediary lists. Use String.split to break the string into KV pairs, then split the KV pair again and add the results to the map:

Please take this all as pseudocode, I did not compile, test, or review it to make sure it didn't have any glaring mistakes. The point is just that you can go directly from the full string (rawInput) and get to the map without creating lots of intermediary lists. You use the String.split to create an array of all Key:Value pairs, then you split each pair and add it to the Map immediately.

Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

Please BeForthrightWhenCrossPostingToOtherSites(⇐ click) so people don't waste their time repeating each others' answers. Thanks.
jagadeesh indukuri

Joined: Apr 02, 2012
Posts: 28

SEE THis class then under stood your problem

Jagadeesh Indukuri, please LetThemDoTheirOwnHomework(⇐click) and DontBeACodeMill.(⇐click) I have removed code you posted.

Also, in the future, when posting code (such as a small snippet as an example, but not, of course, a full solution), please UseCodeTags(⇐click) so it will be readable.

nehad sharma

Joined: Apr 05, 2012
Posts: 2
Thanks everyone for the suggestions. As suggested by you I am using HashMap with String as key and ArrayList<String> as value.
And to implement the multiMap concept, I am using Steve's implementation.
Thanks for making it that simple
I agree. Here's the link:
subject: Creating collections from a String
It's not a secret anymore!