Solved

How to split the values from Multihaspmap ?

Posted on 2013-05-30
4
245 Views
Last Modified: 2013-05-31
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 ?
0
Comment
Question by:Rose_Taylor
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39209418
Try

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

Open in new window

0
 

Author Comment

by:Rose_Taylor
ID: 39209596
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 ?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39209997
System.out.println(me.getKey()+": value :"+list.get(j).toString() .replaceAll("^\\[|\\]$", "").replaceAll(",", " ")); 

Open in new window

0
 

Author Closing Comment

by:Rose_Taylor
ID: 39211471
Good
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 46
How to convert String matching to regex in java 4 34
hibernate example issues from command prompt 10 42
login form jsp example 2 26
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question