Java 地图上的数字列表
我有一张我正在简化的地图,但本质上包含一些数据的关键值,例如:Java 地图上的数字列表,java,sorting,maps,Java,Sorting,Maps,我有一张我正在简化的地图,但本质上包含一些数据的关键值,例如: Yahya, 4 John, 4 Blake, 2 Jill 2 Janoe, 6 Jilly 12 Zapon, 5 Zoe, 4 Hamed, 1 我需要对其进行订购,以便获得以下输出: 1. Jilly, 12 pts 2. Janoe, 6 pts 3. Zapon, 5 pts 4. John, 4 pts 4. Yahya, 4 pts 4. Zoe, 4 pts 7. Blake, 2 pts 7. Jill, 2
Yahya, 4
John, 4
Blake, 2
Jill 2
Janoe, 6
Jilly 12
Zapon, 5
Zoe, 4
Hamed, 1
我需要对其进行订购,以便获得以下输出:
1. Jilly, 12 pts
2. Janoe, 6 pts
3. Zapon, 5 pts
4. John, 4 pts
4. Yahya, 4 pts
4. Zoe, 4 pts
7. Blake, 2 pts
7. Jill, 2 pts
9. Hamed, 1 pts
我已经使用了一个比较器根据值对映射值进行排序:
public <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
Comparator<K> valueComparator = new Comparator<K>() {
public int compare(K k1, K k2) {
int compare = map.get(k2).compareTo(map.get(k1));
if (compare == 0) return 1;
else return compare;
}
};
Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
sortedByValues.putAll(map);
return sortedByValues;
}
公共地图排序值(最终地图){
比较器值比较器=新比较器(){
公共整数比较(kk1,kk2){
int compare=map.get(k2).compareTo(map.get(k1));
如果(比较==0)返回1;
否则返回比较;
}
};
Map sortedByValues=新树映射(valueComparator);
sortedByValues.putAll(地图);
返回SortedByValue;
}
阅读:,还有更多,但不知道如何组合
我知道您可以使用它来获取关键值:
for (Map.Entry<String, Integer> entry : map.entrySet()){
System.out.println(entry.getKey() + " ," + entry.getValue() + " pts");
}
for(Map.Entry:Map.entrySet()){
System.out.println(entry.getKey()+”,“+entry.getValue()+”pts”);
}
但是,缺少两个功能:
Map<String, Integer> map = sortByValues(groupList);
int count = 1;
int counter = 1;
int previousScore = 0;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
//counter = count;
if (previousScore == entry.getValue()) {
System.out.println(counter - 1 + " " + entry.getKey() + "," + entry.getValue() + " pts");
} else {
System.out.println(counter + " " + entry.getKey() + "," + entry.getValue() + " pts");
previousScore = entry.getValue();
count++;
}
counter++;
}
Map Map=sortByValue(组列表);
整数计数=1;
int计数器=1;
int-previousScore=0;
对于(Map.Entry:Map.entrySet()){
//计数器=计数;
if(previousScore==entry.getValue()){
System.out.println(计数器-1+“”+entry.getKey()+,“+entry.getValue()+”pts”);
}否则{
System.out.println(计数器+“”+entry.getKey()+“,”+entry.getValue()+“pts”);
previousScore=entry.getValue();
计数++;
}
计数器++;
}
欢迎您的任何想法。有人能提出一种方法来达到要求的结果吗
ValueComparator
replaceif(compare==0)中,返回1使用if(compare==0)返回k1.compareTo(k2)
这就是我最终得到的,使其能够工作,以获得正确的计数器:
Map <String, Integer>map = sortByValues(leagueTable);
int counter = 1;
int previousScore = 0;
int relativeCount = 0;
int n= 0;
for (Map.Entry<String, Integer> entry : map.entrySet())
{
int value = entry.getValue();
if(previousScore == entry.getValue()){
if(n == 0){
relativeCount = counter-1;
n++;
}
System.out.println(relativeCount + " " + entry.getKey() + "," + entry.getValue() + " pts");
}
else{
n=0;
System.out.println(counter + " " + entry.getKey() + "," + entry.getValue() + " pts");
previousScore = entry.getValue();
}
counter++;
}
Map Map=sortByValue(联盟表);
int计数器=1;
int-previousScore=0;
int relativeCount=0;
int n=0;
对于(Map.Entry:Map.entrySet())
{
int value=entry.getValue();
if(previousScore==entry.getValue()){
如果(n==0){
相对计数=计数器-1;
n++;
}
System.out.println(relativeCount+“”+entry.getKey()+“,”+entry.getValue()+“pts”);
}
否则{
n=0;
System.out.println(计数器+“”+entry.getKey()+“,”+entry.getValue()+“pts”);
previousScore=entry.getValue();
}
计数器++;
}
谢谢您的回答,但是…k1.compareTo(k2)是删除重复值而不是编号,并在之前尝试了各种计数,因此提出了这个问题,因为我无法正确获得它。有相同的想法,但不知道如何实现。将K
声明为Comparable
,方法与V
相同。或者,更好的是,摆脱泛型,只需使用Map
,因为这才是您真正需要的。如果您想让我们帮助您找出问题所在,请显示计数代码。