Results 1 to 6 of 6
  1. #1
    javaworld is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default How to convert hashmap to treeset ?

    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);
    }
    }

    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: How to convert hashmap to treeset ?

    I had to lookup MultiHashMap since its not part of the Java 1.7 JDK. As a side note, I am not certain if it is worth writing a whole new class for something which can be done using HashMap<K, List<V>).

    Anyway, what elements to you want to to convert to a TreeSet? I would assume you can just to something like the following base on Formal Type parameters.

    Java Code:
    Set<V> set = new TreeSet<V>();
    for (K v : map.keySet()) {
         set.addAll(map.get(v));
    }
    Since I don't have MultiHashMap, I will make one to demo:

    Java Code:
    Map<String, Collection<Integer>> map = new  HashMap<>();
        Set<Integer> set = new TreeSet<Integer>();
        for (String key : map.keySet()) {
            set.addAll(map.get(key));
        }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    Default Re: How to convert hashmap to treeset ?

    Hi, have a look at TreeMap - it maintains natural order of the keys and you can pass a Comparator in the constructor if you want some other ordering -> TreeMap (Java Platform SE 6)

    BR,
    Kalin
    Last edited by kalata; 06-01-2013 at 08:43 PM.

  4. #4
    javaworld is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: How to convert hashmap to treeset ?

    Quote Originally Posted by jim829 View Post
    I had to lookup MultiHashMap since its not part of the Java 1.7 JDK. As a side note, I am not certain if it is worth writing a whole new class for something which can be done using HashMap<K, List<V>).

    Anyway, what elements to you want to to convert to a TreeSet? I would assume you can just to something like the following base on Formal Type parameters.

    Java Code:
    Set<V> set = new TreeSet<V>();
    for (K v : map.keySet()) {
         set.addAll(map.get(v));
    }
    Since I don't have MultiHashMap, I will make one to demo:

    Java Code:
    Map<String, Collection<Integer>> map = new  HashMap<>();
        Set<Integer> set = new TreeSet<Integer>();
        for (String key : map.keySet()) {
            set.addAll(map.get(key));
        }
    Regards,
    Jim
    Thanks for reply.

    but Map won't support dublicate values, correct ? if adding dublicating key, its going overwite.Thats reson i have use Multihashmap , its from apache collections.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: How to convert hashmap to treeset ?

    Posted by javaworld
    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.
    You asked how to sort by using a TreeSet. I provide a way to sort the values from your map. MutliHashMap simply maintains a Collection of values for each key.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    javaworld is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: How to convert hashmap to treeset ?

    Okay , My problem is here...

    My SQL returns the values as like below.

    studentid studentname playid gamename grade prizes
    ----------- -------------- ------- ------------ ------- ---------
    121 bob 1 game1 A 1 and 2
    121 bob 2 game2 C 1 and 3
    121 bob 3 game3 B 4 and 2
    121 bob 4 game4 D 1 and 2
    131 jack 3 gam3 A 1
    131 jack 1 game1 A 2 and 3

    I'm getting the result and iterate the values to display.But in the last column need to display the values as different format.

    Iterator<Search> iterator = products.iterator();

    while(iterator.hasNext())
    {
    Search req = (Search)iterator.next();
    req.getStudentid();
    req.getStudentname();
    req.getgameid();
    req.getgamename();
    req.getgrade();
    req.getprizes() ;
    }

    studentid studentname playid gamename prizes
    ---------- -------------- ------- ---------- -------- --------------------------
    121 bob 1 game1 A 1 and 2 and 3 and 4
    121 bob 2 game2 C 1 and 2 and 3 and 4
    121 bob 3 game3 B 1 and 2 and 3 and 4
    121 bob 4 game4 D 1 and 2 and 3 and 4
    131 jack 3 gam3 A 1 and 2 and 3
    131 jack 1 game1 A 1 and 2 and 3

    How to append grade,prizes 4 rows values in first row ? How to loop through here ?
    Last edited by javaworld; 06-02-2013 at 08:30 PM.

Similar Threads

  1. Extract HashMap values to make a TreeSet
    By lannie1980 in forum New To Java
    Replies: 14
    Last Post: 04-25-2012, 10:04 PM
  2. bug? list and treeSet
    By douglas.nelson@oracle.com in forum Advanced Java
    Replies: 34
    Last Post: 04-10-2011, 11:30 PM
  3. Please Help - TreeSet
    By Riftara in forum New To Java
    Replies: 1
    Last Post: 10-21-2010, 08:33 PM
  4. Replies: 7
    Last Post: 12-08-2009, 07:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •