[英]sorting hash map based on interger
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:所以我使用了 stackoverflouw 的其中一个页面,我找到了这个解决方案:
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.这将返回正确的列表,但我不确定这是什么: .limit(1000)
以及如何将其与列表相等,以便我可以返回它。
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.假设您的 HP 类中的 TD1() 方法是您想要排序的值,并且您确实想要使用 Map 来帮助您排序。 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:您可以使用 Java 8 按地图的值进行很好的排序:
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:我选择为重复键(合并函数, Collectors#toMap
的第三个参数)抛出异常,但您也可以只返回找到的第一个键:
.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. HashMap
是一个未排序的映射,不会保证迭代顺序(因此排序将是徒劳的),而TreeMap
是一个SortedMap
,但这在合同上意味着它按键排序,而不是按值排序。 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. LinkedHashMap
将保留迭代顺序,通常基于插入(很像List
),因此当您需要排序映射输出时,通常是您想要的。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.