Results 1 to 12 of 12
  1. #1
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default Collections.sort()

    I have a method that sorts a HashMap by value. It works by turning the values into a list, sorting the list and matching each value on the list with they key in the HashMap. Here it is:
    Java Code:
    public static <E, A> Map<E, A> sortByValue(HashMap<E, A> map)
             {
             Map<E, A> finalMap = new HashMap<E, A>();
            
             List<E> yourMapKeys = new ArrayList<E>(map.keySet());
             List<A> yourMapValues = new ArrayList<A>(map.values());
             List<A> sortedList = new ArrayList<A>(yourMapValues);
             Collections.sort(sortedList);
             int size = sortedList.size();
             for (int i=0; i<size; i++) {
                finalMap.put(yourMapKeys.get(yourMapValues.indexOf(sortedList.get(i))), sortedList.get(i));
                yourMapKeys.remove(yourMapValues.indexOf(sortedList.get(i)));
                yourMapValues.remove(yourMapValues.indexOf(sortedList.get(i)));
                }
             return finalMap;
             }
    The problem is that i get a compiler error:
    cannot find symbol
    symbol : method sort(java.util.List<Integer>)
    location: class java.util.Collections
    Collections.sort(sortedList);


    What is wrong? the API says Collections supports sort(List<T> list) so why the error?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Are you sure you don't get

    Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (List<A>). The inferred type A is not a valid substitute for the bounded parameter <T extends Comparable<? super T>> instead? Because that is what I get which should help you understand why it's not allowed.

    P.S Why not use a TreeMap?

  3. #3
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Because I thought a TreeMap sorted by key. Anyway, when I make a List<Integer> and sort it it works. If I know that the values will always be integers is there a way to make it so A = Integer and E is dynamic?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Better read the reply I posted again. T is clearly described as

    "T extends Comparable<? super T>".

    Otherwise, how is the method going to be able to sort objects which cannot be compared?

  5. #5
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Integers can be compared...

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by collin389 View Post
    Integers can be compared...
    But your list is a List<A>. The compiler can't verify that all As passed to the method are going to be comparable. Remember the check is happening at compile time not at run time. For the method to always work correctly, make your A according to the required specification. i.e make your method signature as
    Java Code:
    public static <E, A extends Comparable<? super A>> Map<E, A> sortByValue(HashMap<E, A> map) {
    That way, the method guards against wrong types because no one can call it passing a non-comparable A.

  7. #7
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Thanks very much. Also, I meant how could i make the value of the map an Integer (so it would be comparable, plus thats all I need) but the key could be anything <A>

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Sorting the values does seem a bit odd to me. Are you sure your design is correct (i.e should you perhaps use your keys as values and vice versa instead?).

    What's the point of sorting the values in a mapping when you can only get the values through the keys?

  9. #9
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    I want to sort the Integers, but they can be repeating and map doesn't support repeating keys.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Like I said, I don't really see the reason why you are sorting the values. Maybe I'm not understanding your reasons. Good luck with the rest of it.

  11. #11
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Well.... Quote from Sun:"This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time."
    lol figured that out the hard way.
    Oh well, I decided I just needed the keys so I turned those into and array and returned that(for a different function that this whole sort thing is for the example method is pretty useless).

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    ... and if you want the keys to be always sorted then use the TreeMap.

Similar Threads

  1. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 01:03 AM
  2. Collections Help
    By Dr Gonzo in forum New To Java
    Replies: 0
    Last Post: 12-07-2008, 09:15 PM
  3. Collections Sort
    By senthil_jr in forum Advanced Java
    Replies: 2
    Last Post: 06-04-2008, 08:11 AM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  5. Collections framework
    By yuvi461 in forum New To Java
    Replies: 2
    Last Post: 01-08-2008, 10:46 AM

Posting Permissions

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