-4

I have a following values in map

john smith=mr  
kim taylor=prof   
shannon sperling=miss  

I wanted to convert into the following format in arraylist

mr | john smith  
prof | kim taylor  
miss | shannon sperling

i tried with following code

--------------------
---------------------
Map<String, String> title = gluetitles(titles);
        ArrayList<Entry> list1 = new ArrayList<Entry> (title.entrySet());
        System.out.println(list1);
---------------------
---------------------

But i did not get idea how to get the above format...

5
  • Can you show me how you are storing couples <String, String> in the map? Commented Nov 8, 2013 at 12:31
  • Please, be more specific. Add more explanation of what you trying to achieve. Commented Nov 8, 2013 at 12:32
  • The persons in your example seem to be important for a lot of code :O stackoverflow.com/questions/19831598/… Commented Nov 8, 2013 at 12:33
  • Do you need to use a Map? The same can be achieved just by splitting and reformatting the String, then adding that String to the list. Commented Nov 8, 2013 at 12:33
  • @peeskillet: i tried in that way.But i had problems with casting.Anyhow one of the following answer solves my problem. Commented Nov 8, 2013 at 13:13

3 Answers 3

1

Well, I'm not sure what exactly you aim for, but this will output something that is closer to what you wanted than what you had (as far as I understand your goal).

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Map<String, String> hm = new HashMap<>();
        hm.put("john smith", "mr");
        hm.put("kim taylor", "prof");
        hm.put("shannon sperling", "miss");
        List<String> list = new ArrayList<String>();
        for (String key : hm.keySet())
            list.add(hm.get(key) + " | " + key);
        for (String s:list)
            System.out.println(s);
    }

}

This will print out:

miss | shannon sperling
prof | kim taylor
mr | john smith
Sign up to request clarification or add additional context in comments.

Comments

0

This may help you

    Map<String,String> map=new HashMap<>();
    List<String> list=new ArrayList<>();

    for (Map.Entry<String,String> i:map.entrySet()){
           list.add(i.getValue()+" | "+i.getKey());
    }

Comments

0

You would need to handle the formatting yourself:

Map<String, String> title = gluetitles(titles);
List<String> list = new ArrayList<String> (title.size());
for (Map.Entry <String, String> entry: title.entrySet()) {
    list.add (title.getValue() + " | " + title.getKey();
}
System.out.println(list);

2 Comments

Actually i tried like this,but i got error " method getKey() is undefined for the type Map<String,String>". My eclipse suggests to add cast title to "Entry<String, String>". After casting the error is "HashMap cannot be cast to java.util.Map$Entry"
You should not iterate on Map<String,String>, but on Map.Entry<String,String>, as my answer proposes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.