Solved

How to convert hashmap to treeset ?

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I remove an object from a 3 58
Facing this issue for maven proxy setting 2 39
ejb on wildfly 5 60
java mysql insert application 14 64
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 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.
Suggested Courses

736 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