Solved

How to convert hashmap to treeset ?

Posted on 2013-05-31
5
595 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 26

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 26

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 84
java 8 lambda expresssions exception handling 3 90
servlet filter example 37 63
jsp login check 12 31
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

773 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