Solved

How to convert hashmap to treeset ?

Posted on 2013-05-31
5
615 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

679 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