Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Sorting a hashmap

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 10-02-2010
Member
 
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.

Reply With Quote
  #2  
Old 10-02-2010
Member
 
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();
  }
}
Reply With Quote
  #3  
Old 10-02-2010
Member
 
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());
  }
}
Reply With Quote
  #4  
Old 10-02-2010
Member
 
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.
Reply With Quote
  #5  
Old 10-02-2010
Member
 
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).
Reply With Quote
  #6  
Old 10-02-2010
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Sorting a hashmap"
Thread Thread Starter Forum Replies Last Post
HashMap vs HashTable Joyjeet Software Development 6 04-01-2011 09:07 PM
Design calculator with HashMap Angelica Maria Software Development 4 25-03-2010 02:47 PM
Associated values with HashMap Miles Runner Software Development 5 13-02-2010 03:50 AM
Iteration over a hashmap New ID Software Development 5 07-02-2010 03:40 AM
What is HashMap and Map? TAIPA Software Development 4 27-02-2009 11:16 PM


All times are GMT +5.5. The time now is 06:22 PM.