-1

Currently I have a map containing key as a string & values as a list of strings. Map>

How can I convert these values from list of strings to a single string? eg: Below are the values which are contained in my map .

{id=[1057-27620], name=[apple,mango,banana], color=[apple:red, mango:yellow, banana:black&yellow], place=[greece]}

I want the above to be stored in Map<String,String>as

{id="1057-27620", name="apple,mango,banana", color="apple:red, mango:yellow, banana:black&yellow", place="greece"}

How do I do that?

user3561824
  • 139
  • 1
  • 7
  • Have you tried anything? – Rohit Jain Apr 25 '14 at 06:25
  • 1
    Iterating and building a String with `StringBuilder` - what have you tried so far? – stuXnet Apr 25 '14 at 06:26
  • Any code yet ? hint: walkthrough your list and concatenate strings to a single string and add to another map – Sanjeev Apr 25 '14 at 06:26
  • The question is why do you want to concatenate strings which are different?. The next time you want to use these values as seperate strings, you will have to split them again. – TheLostMind Apr 25 '14 at 06:27
  • two different strings? I want to convert the values which are in form of a list to one string, so that I won't have to iterate through the list while printing out the key - value pairs in freemarker. I basically want a single key & infront of that multiple values as a string. Once I have that in a map , I can then use it to iterate in freemarker – user3561824 Apr 25 '14 at 06:32

3 Answers3

0

The problems comes down to how to convert from List<String> to the String containing the element separated by comma. Well, use Guava's Joiner here.

Here's an example:

List<String> list = Arrays.asList("aabc", "bcd", "aba");
String listToString = Joiner.on(", ").join(list); 
System.out.println(listToString);   // "aabc, bcd, aba"

I would leave the job of applying this to convert Map<String, List<String>> to Map<String, String> on you.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
0

Try giving all values inside" " and seperate it using ,

-1

get list of string and apply List.toString().replaceAll("[","").replaceAll("]",""); and build another map

niiraj874u
  • 2,180
  • 1
  • 12
  • 19