Results 1 to 12 of 12
Thread: Collections.sort()
- 12-31-2009, 06:45 AM #1
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 12
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; }
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?
- 12-31-2009, 07:28 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
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?
- 12-31-2009, 07:33 AM #3
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 12
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?
- 12-31-2009, 07:38 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
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?
- 12-31-2009, 07:39 AM #5
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 12
Integers can be compared...
- 12-31-2009, 07:49 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
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) {
- 12-31-2009, 08:01 AM #7
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 12
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>
- 12-31-2009, 08:07 AM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
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?
- 12-31-2009, 08:11 AM #9
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 12
I want to sort the Integers, but they can be repeating and map doesn't support repeating keys.
- 12-31-2009, 08:15 AM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
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.
- 12-31-2009, 10:59 AM #11
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 12
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-31-2009, 11:03 AM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
Similar Threads
-
Using Merge Sort to sort an ArrayList of Strings
By coldfire in forum New To JavaReplies: 3Last Post: 03-13-2009, 01:03 AM -
Collections Help
By Dr Gonzo in forum New To JavaReplies: 0Last Post: 12-07-2008, 09:15 PM -
Collections Sort
By senthil_jr in forum Advanced JavaReplies: 2Last Post: 06-04-2008, 08:11 AM -
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM -
Collections framework
By yuvi461 in forum New To JavaReplies: 2Last Post: 01-08-2008, 10:46 AM
Bookmarks