如何在Java中对HashMap进行排序?
我正在使用一个如何在Java中对HashMap进行排序?,java,hashmap,bukkit,Java,Hashmap,Bukkit,我正在使用一个HashMap作为一种定时投票系统。其中,字符串是对象的名称,整数是该对象的投票数。我想做的是对整数进行降序排序,如果他们是平手,我想选择之前没有赢得投票的(如果他们中的任何一个赢了) 我尝试使用一个树映射,但它似乎没有达到我想要的效果,因为它根据键的值进行排序,而我需要对值进行排序。也不起作用,因为有时两个对象可能都有相同的投票数。摘自,下面是如何使用JDK 8按值(降序)对映射进行排序: public static <K, V extends Comparable<
HashMap
作为一种定时投票系统。其中,字符串是对象的名称,整数是该对象的投票数。我想做的是对整数进行降序排序,如果他们是平手,我想选择之前没有赢得投票的(如果他们中的任何一个赢了)
我尝试使用一个树映射,但它似乎没有达到我想要的效果,因为它根据键的值进行排序,而我需要对值进行排序。也不起作用,因为有时两个对象可能都有相同的投票数。摘自,下面是如何使用JDK 8按值(降序)对映射进行排序:
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
return map.entrySet().stream().sorted(Map.Entry.comparingByValue(Collections.reverseOrder())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
publicstatic要确定平局的结果,您需要的不仅仅是一个整数。一种解决方案是创建一个自定义对象,该对象保存额外的信息并实现可比性(类似于Walter所说的)
从你的帖子中我可以看出,当出现平局投票时,你希望结果是一个没有像其他平局选项那样最近被选中的选项。如果是这种情况,那么下面的解决方案(使用日期作为辅助信息)应该可以工作
import java.util.Date;
public class VoteOption implements Comparable<VoteOption>{
private String name;
private Integer votes;
private Date lastVote;
/** Constructor */
public VoteOption(String name){
this.name = name;
this.lastVote = new Date();
this.votes = 0;
}
/** gets the name of this option */
public String name(){
return this.name;
}
/** gets the number of votes this option currently has */
public int votes(){
return this.votes;
}
/** Call this method if the vote passed with this option.
* It will update the lastVote date so that this will become the
* last option to be picked if there is a tie in the next vote. */
public void votePassed(){
this.lastVote = new Date();
}
/** resets the vote count back to 0 */
public void resetVoteCount(){
this.votes = 0;
}
/** Adds 1 vote to the vote count */
public void vote(){
this.votes ++;
}
@Override
public int compareTo(VoteOption otherOption){
int compareVotes = this.votes.compareTo(otherOption.votes);
if(compareVotes!=0){
return compareVotes;
} else {
//handle vote ties
int compareDates = this.lastVote.compareTo(otherOption.lastVote);
return compareDates;
}
}
}
你能举例说明你的代码吗?你可以使用你为它设计的一个简单的排序算法,但如果你把你的一些代码放进去,它会很有帮助。你能确切地说明如何打破这种联系吗?i、 e.如果有3票支持保罗,2票支持安,安被投了票,那么安需要在排序中出现在保罗之前,因为保罗是之前投票的人。这是正确的吗?如果安(有2票)被投票时,有更多的人投了3票(保罗除外)(保罗是之前被投票的人),那会怎么样?请澄清这一切,以得到一个好的答案。似乎你不能这样做只是排序整数。您需要将信息移动到带有整数和其他标志的对象。然后,在排序算法将需要使用此标志来完成您的逻辑。重复的。看看这个问题的解决方案。按价值倒序排序是好的,但打破平局又如何?@Federicoperaltachaffner对于一个轮胎,我不太明白OP对于他们之前是否赢得投票意味着什么。是否存在存储信息的变量?如果OP可以澄清,那么我可以编辑我的答案。我的理解是,如果有3票支持保罗,2票支持安,并且安被投票,那么安需要在排序中出现在保罗之前,因为保罗是之前错过投票的人。也许@ctooley17可以澄清这一点…@JacobG。是的,将有一个字符串对象存储以前赢得投票的对象的名称。所以,基本上,两个物体之间是否存在平局,我想确保他们之前都没有赢过,如果他们赢了,我想挑一个没有赢的,如果他们都没有赢,那么我就随机挑一个。@FedericoPeraltaSchaffner我的意思是说,在第一轮保罗得到3票,安得到2票,保罗赢了,但在第二轮,如果保罗和安各得到3票,然后选择安,因为保罗上次赢了。
import java.util.Date;
public class VoteOption implements Comparable<VoteOption>{
private String name;
private Integer votes;
private Date lastVote;
/** Constructor */
public VoteOption(String name){
this.name = name;
this.lastVote = new Date();
this.votes = 0;
}
/** gets the name of this option */
public String name(){
return this.name;
}
/** gets the number of votes this option currently has */
public int votes(){
return this.votes;
}
/** Call this method if the vote passed with this option.
* It will update the lastVote date so that this will become the
* last option to be picked if there is a tie in the next vote. */
public void votePassed(){
this.lastVote = new Date();
}
/** resets the vote count back to 0 */
public void resetVoteCount(){
this.votes = 0;
}
/** Adds 1 vote to the vote count */
public void vote(){
this.votes ++;
}
@Override
public int compareTo(VoteOption otherOption){
int compareVotes = this.votes.compareTo(otherOption.votes);
if(compareVotes!=0){
return compareVotes;
} else {
//handle vote ties
int compareDates = this.lastVote.compareTo(otherOption.lastVote);
return compareDates;
}
}
}
Collections.sort(list);