Sorting A HashMap By Values

Discussion in 'Plugin Development' started by Tster, Dec 13, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Tster

  2. A HashMap is unsorted. A TreeMap, for example, is sorted.
     
    Father Of Time likes this.
  3. Offline

    Father Of Time

    As stated by V10lator, there are collections that are designed specifically for object sorting, you might want to invest a little time familiarizing yourself with the differences between the different collection types in java.
     
  4. Offline

    Brain

    TreeMap would be a good start, however it sorts by key and not by value. I'm not sure if there is a data structure in the default Java 1.6 API that fills your requirements.

    Perhaps if you describe what you intend to do with such a data structure someone might have an idea how to solve your problem another way.
     
  5. Offline

    Tster

    I have a list of variables that need to be sorted lowest to highest, but I need to keep the variable name for an 'if' statement
     
  6. Offline

    halley

    As V10lator and others have said, a Map is by itself not sorted. However, it's a common need, and I think I was the one who suggested the OP look up "sorting a map by values" yesterday.

    There are straightforward techniques in many languages to do this, and Java is no different. Essentially you make an array of the map entries, then a custom compare() function allows the sorting routine to decide which entries come first. An excellent code snippet for Map<String,String> is at this page: http://www.xinotes.org/notes/note/306/ - if you want to sort a Map<String,Integer> by the integers, then this should work as-is. You won't even have to adjust the compare() routine unless you wanted a more complicated sort.
     
    Father Of Time likes this.
  7. Offline

    Father Of Time

    Well put Halley.
     
  8. Offline

    Sagacious_Zed Bukkit Docs

  9. Offline

    Brain

    The custom comparator in TreeMap runs on the keys. You could use the keys provided to pull the values when implementing the comparator. However I'm not sure if this works properly when inserting data. But it's worth a try before involving multiple data structures.
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    @Brain
    your right not thinking, keys not values....
     
  11. Offline

    Tster

    Hmm, then I could store the integer as a key, so it can be sorted with a treemap
     
  12. Offline

    Brain

    Yes and no. Keys must be unique. If you put the same Integer as key twice, the second key's value (String) will replace the first.
    If your Integers are unique you could maintain a second Hashmap<String, Integer> to speed up access to individual entries and then use the TreeMap to access ranges of Integers.
     
  13. Offline

    Tster

    Ok, I have already stated in my config that the integers have to be unique because otherwise my percentage generating algorithm may not work
     
  14. Offline

    Brain

    Excellent, then go for it! :cool:
     
  15. Offline

    Tster

    Is this a valid method:?
    Code:
    TreeMap sortedoremap = new TreeMap(oremap);
     
  16. Offline

    Brain

    If oremap is a Map, yes.
     
  17. Offline

    Tster

    So am I right this sortsit by integer key lowest to highest
     
  18. Offline

    Brain

    Yes. I'd use generics though, helps prevent future headaches.
    Code:
    TreeMap<Integer, String> sortedOreMap = new TreeMap<Integer, String>(oremap);
    and oremap should be something like
    Code:
    HashMap<Integer, String> oremap = new HashMap<Integer,String>();
     
  19. Offline

    Tster

    Does it matter if oremap is a treemap? or is a HashMap just more optimized for something like this?
    Thanks alot @Brain, you have helped the most so far, and I will list you as a contributor, because if this wouldn't work, no configuration in my plugin would be pathetic
     
  20. Offline

    Brain

    Here, let me show you: http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html

    This is one of the constructors: TreeMap(Map<? extends K,? extends V> m)

    What is important if you want to create a TreeMap that is initially filled with content that you use another datastructure that implements the Map interface - which Treemap does (see "All Implemented Interfaces")

    In answer to your question: Yes, that's quite alright.
     
Thread Status:
Not open for further replies.

Share This Page