Solved

How to convert hashmap to treeset ?

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 86
wordlen challenge 3 47
eclipse compiler vs Installed JREs option 3 40
ArrayIndexOutOfBoundException 9 35
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

760 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

20 Experts available now in Live!

Get 1:1 Help Now