Link to home
Start Free TrialLog in
Avatar of Rose_Taylor
Rose_TaylorFlag for United States of America

asked on

How to split the values from Multihaspmap ?

public static void main(String[] args) {
                      MultiHashMap mp=new MultiHashMap();
                      StringBuilder b = new StringBuilder();
                      mp.put("a", 10);
                      mp.put("a", 11);
                      mp.put("a", 12);
                      mp.put("b", 13);
                      mp.put("c", 14);
                      mp.put("e", 15);
                      mp.put("b", 1);
                      mp.put("b", 2);
                      mp.put("b", 3);
                      List list = null;

                      Set set = mp.entrySet();
                      Iterator i = set.iterator();
                      while(i.hasNext()) {
                         Map.Entry me = (Map.Entry)i.next();
                         //list=(List)mp.get(me.getKey());
                         System.out.println("key:"+me.getKey());
                         System.out.println("value:"+me.getValue());
}
}

Above program output is :

key:e  value:[15]
key:b  value:[13, 1, 2, 3]
key:c  value:[14]
key:a  value:[10, 11, 12]

I want to print the values like
15
13 1 2 3
14
10 11 12

How to print the values as like above format ?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Try

System.out.println(me.getKey());
System.out.println(me.getValue().replaceAll("^\\[|\\]$", "").replaceAll(",", " "));

Open in new window

Avatar of Rose_Taylor

ASKER

Great. Thanks for post.

public static void main(String[] args) {
                      MultiHashMap mp=new MultiHashMap();
                      StringBuilder b = new StringBuilder();
                      mp.put("a", 10);
                      mp.put("a", 11);
                      mp.put("a", 12);
                      mp.put("b", 13);
                      mp.put("c", 14);
                      mp.put("e", 15);
                      mp.put("b", 1);
                      mp.put("b", 2);
                      mp.put("b", 3);
                      List list = null;

                      Set set = mp.entrySet();
                      Iterator i = set.iterator();
                      while(i.hasNext()) {
                         Map.Entry me = (Map.Entry)i.next();
                         list=(List)mp.get(me.getKey());
                        
                         for(int j=0;j<list.size();j++)
                         {
                             
                          System.out.println(me.getKey()+": value :"+list.get(j));
                         }
                      }
                      
                   }

Above program output is :

e: value :15
b: value :13
b: value :1
b: value :2
b: value :3
c: value :14
a: value :10
a: value :11
a: value :12

I would like to get values :

e: value :15
b: value :13 1 2 3
b: value :13 1 2 3
b: value :13 1 2 3
b: value :13 1 2 3
c: value :14
a: value :10 11 12
a: value :10 11 12
a: value :10 11 12

How to get the values as like above ?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good