This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating collections from a String

 
nehad sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
Steps:
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
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please BeForthrightWhenCrossPostingToOtherSites(⇐ click) so people don't waste their time repeating each others' answers. Thanks.

https://forums.oracle.com/forums/thread.jspa?threadID=2369871&tstart=0
 
jagadeesh indukuri
Greenhorn
Posts: 28
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SEE THis class then under stood your problem

MODERATOR EDIT:
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.

Thanks!
 
nehad sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic