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
Solved

How to convert hashmap to treeset ?

Posted on 2013-05-31
5
602 Views
Last Modified: 2013-06-05
Hello,

How to convert hashmap to tree in below code. I need to use treeset instead of set.
Treeset maintain natual ordeing , so that i would like to change it to treeset.

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()); // get the GROUP here

                          // print out the SAME GROUP a bunch of different times
                          int itemCount = list.size();
                          for (int z = 0; z < itemCount; z++) {
                              // and then print out the the values ..
                              String values = "";
                              for(int j=0;j<list.size();j++)
                              {
                                  values += list.get(j);
                              }
                              System.out.println(me.getKey() + ": value :" + values);
                           }
                       }
                      
                   }
0
Comment
Question by:Rose_Taylor
  • 2
  • 2
5 Comments
 
LVL 27

Accepted Solution

by:
dpearson earned 500 total points
ID: 39212383
Rather than a MultiHashMap I think you may want to try using Guava's TreeMultiMap:
http://guava-libraries.googlecode.com/svn/tags/release09/javadoc/com/google/common/collect/TreeMultimap.html

If you're not familiar with Guava it's a set of libraries from Google that's become very popular in the Java world: https://code.google.com/p/guava-libraries/

Doug
0
 

Author Comment

by:Rose_Taylor
ID: 39221032
I've requested that this question be deleted for the following reason:

I didn't get any answer for my question.I did it myself.
In goole collections , Treemultihaspmap i have implemented.Its working good.
Thanks for support.
0
 
LVL 27

Expert Comment

by:dpearson
ID: 39221033
Um isn't that exactly what I suggested using - Google's TreeMultiMap?  It sounds like you followed my advice and it worked perfectly.

I'm not sure why you didn't see that as an answer to your question?

Doug
0
 

Author Closing Comment

by:Rose_Taylor
ID: 39222875
good
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 116
maven disable workspace resolution 1 21
ejb mdb examples 1 8
Selenium docs api java index 3 48
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

839 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