Trying to sort a list in descending order, so longest time first. This is my method I used few pages from here to make it correct however something in my code is wrong and it returns the list which is not so correct.
public static ArrayList<String> winnerIs(List<HP> hp){
//System.out.println("1");
int size = hp.size();
//System.out.println(size);
ArrayList<HP> listofWinner = new ArrayList<HP>();
Map<String, Integer> map = new HashMap<String, Integer>();
for(int i = 0; i < size; i++){
listofWinner.add(hp.get(i));
map.put(hp.get(i).getName(), hp.get(i).TD1());
//System.out.println(hp.get(i).getName()+" "+hp.get(i).TD1());
}
//sort based on time
ArrayList<String> keys = new ArrayList<String>(map.keySet());
//System.out.println("---------------");
/*for(int i = 0; i < keys.size(); i++){
//wn.add(keys.get(i));
System.out.println("here "+keys.get(i));
}*/
//System.out.println("---------------");
ArrayList<String> wn = new ArrayList<String>();
//System.out.println("---------------");
for(int i = keys.size()-1; i >= 0; i--){
wn.add(keys.get(i));
}
return wn;
}
here is what it reurns:
[team2, team1, team4, team3]
but it should be like this:
[team4, team3, team2, team1]
it doesn't matter if the time is equal, we just need the better time, I am not sure what part of the code is wrong.
even when I use this
ArrayList<Integer> s = new ArrayList<Integer>(map.values());
Collections.sort(keys);
//System.out.println("---------------");
for(int i = 0; i < s.size(); i++){
//wn.add(keys.get(i));
System.out.println("here "+s.get(i));
}
the result are still not correct here is what it returns:
here 2
here 9
here 0
here 0
so I used one of the pages at stackoverflouw and I found this solution:
public static ArrayList<String> winnerIs(List<HumanPlayer> hp){
//System.out.println("1");
int size = hp.size();
//System.out.println(size);
ArrayList<HumanPlayer> listofWinner = new ArrayList<HumanPlayer>();
Map<String, Integer> map = new HashMap<String, Integer>();
for(int i = 0; i < size; i++){
listofWinner.add(hp.get(i));
map.put(hp.get(i).getName(), hp.get(i).getTimeDriver1());
//System.out.println(hp.get(i).getName()+" "+hp.get(i).getTimeDriver1());
}
map.entrySet().stream()
.sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
.limit(1000)
.forEach(System.out::println);
return null;
}
this returns the correct list but I am not sure what is this: .limit(1000)
and also how can I equal this to a list, so I can return it.
Assuming the TD1() method in your HP class is the value you want to sort against, and that you really want to use a Map to help you sort. I think you want something like this
Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
for (HP h : hp) {
if (map.get(h.TD1() != null) {
map.get(h.TD1()).add(h.getName());
}
else {
List temp = new ArrayList<String>();
temp.add(h.getName());
map.put(h.TD1(), temp);
}
}
ArrayList keys = Arrays.asList(map.getKeyset().toArray());
Collections.sort(keys);
for ( int i = keys.length() - 1; i >= 0; i--) {
List<String> names = map.get(i);
// print names
}
You can use Java 8 for a nice sorting by value of the map:
Map<String, Integer> sorted = /* your map */.entrySet().stream()
.sorted(Entry.comparingByValue()) //comparator for value, can reverse or use other
.collect(Collectors.toMap(Entry::getKey, Entry::getValue,
(e1, e2) -> { throw new IllegalArgumentException("Duplicate Key: " + e1.getKey()); },
LinkedHashMap::new));
I chose to throw an exception for a duplicate key (the merging function, the 3rd argument of the Collectors#toMap
), but you can also just return the first key found:
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
The thing to keep in mind is the contracts that individual maps uphold. HashMap
is an unsorted map and will not guaruntee iteration order (so sorting would be a fruitless endeavor), and a TreeMap
is a SortedMap
, but that contractually means it's sorted by key, not value. A LinkedHashMap
will preserve iteration order, usually based on insertion (much like a List
), and thus is usually what you want when you need to have a sorted map output.
Check this psuedo-code out, I believe you would get overall idea looking at this.
Map<Integer,List<String> map = new HashMap<Integer,List<String>>();
for(HP hpObject:hp) {
if(map.containsKey(hpObject.TD1())) {
map.get(hpObject.TD1()).add(hpObject.getName());
} else {
List<String> names = new ArrayList<String>();
names.add(hpObject.getName());
map.put(hpObject.TD1(),names);
}
}
// To sort by keys
TreeMap sortedByTD = new TreeMap(map);
// Iterate over TreeMap and create the list of winners you need
return result;enter code here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.