?
Solved

How to split the values from Multihaspmap ?

Posted on 2013-05-30
4
Medium Priority
?
249 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 1500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

777 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