Results 1 to 6 of 6

Thread: Sorting a hashmap

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    Sorting a hashmap

    Hello,
    I am new to java and I want to know how we sort a HashMap. This is the piece of code I have with me.
    Code:
    private static void Save(String Output) throws Exception
        {
        		PrintWriter pw = new PrintWriter(new File (Release));
    Object[] tab = map.keySet().toArray();
    			for(int i = 0; I <tab.length; I + +)
    			{
    				pw.System.out.println(tab[i]+" " + Map.get(tab[i]));
    pw.flush();
    			}
    			pw.close();
    	}
    I try and create a file "exit" and that is sort in descending order by report. But this is not working for me. If you have some ideas on this it will help me.

  2. #2
    Join Date
    Nov 2009
    Posts
    356

    Re: Sorting a hashmap

    Hello,
    To sort a HashMap, you can either use a TreeMap or put your values in your set values in a List that you sort. Also in your code if you have an exception then the PrintWriter is not closed. Use something like:
    Code:
    private static void Save(String Output) throws Exception {
      PrintWriter pw = null;
      try {
        pw = new PrintWriter(new File (Release));
        Object[] tab = map.keySet().toArray();
        for(int i = 0; I <tab.length; I + +) {
          pw.System.out.println(tab[i]+" " + Map.get(tab[i]));
          pw.flush();
        }
      } finally {
        if (pw! = null) {
          pw.close();
      }
    }

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: Sorting a hashmap

    Hey,
    The above posted is correct but I think it needs a modification to the code. I have modified and changed some of the things in the code and the code is now more effective. Here is the code
    Code:
    Public static void hand(Final String[] args) {
      / / Map Testing
      Final Map m = <String, Integer> new HashMap <String, Integer>();
      m.could("t1", 1);
      m.could("t2", 3);
      m.could("t3", 2);
      m.could("t4", 3);
     
      / / Add entries to m a list
      Final List <Entry <String, Integer>> entries = new ArrayList <Entry <String, Integer>>(m.entrySet());
     
      / / Sort the list on the entry value
      Collections.fate(entries, new Comparator <Entry <String, Integer>>() {
        Public int compares(Final Entry <String, Integer> e1, Final Entry <String, Integer> e2) {
          return e1.getValue().compareTo(e2.getValue());
        }
      });
     
      / / Display result
      for (Final <String, Integer> Entry entry: entries) {
        System.out.System.out.println(entry.getKey() + " " + Entry.getValue());
      }
    }

  4. #4
    Join Date
    Dec 2009
    Posts
    296

    Re: Sorting a hashmap

    Hi,
    Thank you for your answers but I do not quite understand your code ,in fact as I have said in the beginning that I am a beginner and I know too work with java (if not too much to ask ) m you explain what is the "Entry" t that you put here:
    / / Add en to map a list
    Final List <Entry <String, Integer>> en = new ArrayList <Entry <String, Integer>>(map.entrySet());
    I also want to understand what is the "e1" and "e2" for you in the sort
    Code:
    function
     / / Sort the list on the entry value
      Collections.fate(en, new Comparator <Entry <String, Integer>>() {
        Public int compares(Final Entry <String, Integer> e1, Final Entry <String, Integer> e2) {
          return e1.getValue().compareTo(e2.getValue());
        }
    Any help on this please.

  5. #5
    Join Date
    Nov 2009
    Posts
    359

    Re: Sorting a hashmap

    Hello,
    Entry is a static class Map. It allows the key-value relationship. The <> are a feature of Java 5: generics. If you do not know what it is, I advise you to read docs on it. The method compares takes two input values. These values are the type of objects in the list. So if the list is composed of one, the method compares needs to take one input. e1 and e2 are just the names of the entries.
    e1.getValue () retrieves the value in the combination key / value (ie Entry).

  6. #6
    Join Date
    Nov 2009
    Posts
    343

    Re: Sorting a hashmap

    Hello,
    The sorting is done in the method compares:
    Code:
     / / Sort the list on the entry value
      Collections.fate(en, new Comparator <Entry <String, Integer>>() {
        Public int compares(Final Entry <String, Integer> e1, Final Entry <String, Integer> e2) {
          return e1.getValue().compareTo(e2.getValue());
        }
      });
    I hope your problem is solved.

Similar Threads

  1. HashMap vs HashTable
    By Joyjeet in forum Software Development
    Replies: 6
    Last Post: 04-01-2011, 08:07 PM
  2. Design calculator with HashMap
    By Angelica Maria in forum Software Development
    Replies: 4
    Last Post: 25-03-2010, 01:47 PM
  3. Associated values with HashMap
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 02:50 AM
  4. Iteration over a hashmap
    By New ID in forum Software Development
    Replies: 5
    Last Post: 07-02-2010, 02:40 AM
  5. What is HashMap and Map?
    By TAIPA in forum Software Development
    Replies: 4
    Last Post: 27-02-2009, 10:16 PM

Posting Permissions

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