Solved

How to split the values from Multihaspmap ?

Posted on 2013-05-30
4
241 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now