Solved

How to split the values from Multihaspmap ?

Posted on 2013-05-30
4
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

730 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