Java 基于对象键的2个值对hashmap进行排序
比如说,Java 基于对象键的2个值对hashmap进行排序,java,Java,比如说, Map<Testing, Integer> xx = new EnumMap<>(Testing.class); public enum Testing { EXAMPLE_1(1, 2); EXAMPLE_2(2, 100); EXAMPLE_3(2, 20); public int position; public int value; public Testing(final int pos, fin
Map<Testing, Integer> xx = new EnumMap<>(Testing.class);
public enum Testing {
EXAMPLE_1(1, 2);
EXAMPLE_2(2, 100);
EXAMPLE_3(2, 20);
public int position;
public int value;
public Testing(final int pos, final int value) {
this.position = pos;
this.value = value;
}
}
我希望结果返回为{EXAMPLE_2,EXAMPLE_3,EXAMPLE_1)
我尝试过使用集合进行两次排序,但它似乎不起作用,因为它只参与最后一次排序
Eg:
Collections.sort(Map, new DescendingPosition());
Collections.sort(Map, new AscendingValue()); // only this applies
提前谢谢
编辑*
这是我已经测试过的,它是有效的,有没有更短的方法可以做到
Map<Testing, Integer>[] hi2 = new TreeMap<>[3];
for (Entry<Testing, Integer> hmm : statups.entrySet()) {
final int pos = (hmm.getKey().position - 1);
if (hi2[pos] == null) {
hi2[pos] = new TreeMap<>(new CompareValue());
hi2[pos].put(hmm.getKey(), hmm.getValue());
} else {
hi2[pos].put(hmm.getKey(), hmm.getValue());
}
}
Map<Testing, Integer> finalMap = new LinkedHashMap<>();
for (int i = (hi2.length - 1); i >= 0; i--) { // to arrange positions in descending
if (hi2[i] != null) {
finalMap.putAll(hi2[i]);
}
}
System.out.println(finalMap);
public static class CompareValue implements Comparator<Testing> {
@Override
public int compare(Testing o1, Testing o2) {
return o1.value - o2.value;// ascending values
}
}
Map[]hi2=新树映射[3];
for(条目hmm:statups.entrySet()){
final int pos=(hmm.getKey().position-1);
如果(hi2[pos]==null){
hi2[pos]=新树映射(new CompareValue());
hi2[pos].put(hmm.getKey(),hmm.getValue());
}否则{
hi2[pos].put(hmm.getKey(),hmm.getValue());
}
}
Map finalMap=newlinkedhashmap();
for(inti=(hi2.length-1);i>=0;i--){//以降序排列位置
if(hi2[i]!=null){
最终地图putAll(hi2[i]);
}
}
系统输出打印(finalMap);
公共静态类CompareValue实现Comparator{
@凌驾
公共整数比较(测试o1,测试o2){
返回o1.value-o2.value;//递增值
}
}
HashMap
是无序的,并且只在列表上工作
您可能想在这里使用a而不是HashMap
,但它是根据键[而不是值]排序的。[我不确定我是否理解,但是如果您试图根据键排序,您可以让您的类实现或实现您自己的类,TreeMap
可能最适合这里]
另一种选择是使用[并且必须做更多的工作].LinkedHashMap
按照插入元素的顺序维护元素,因此您必须自己维护。请注意,如果您频繁插入元素,则可能效率不高,因为维护此结构将耗费大量时间。哈希映射是无序的,因此无法对其进行排序。
但假设您只想按排序顺序从HashMap中提取键,请使用Collection.sort(yourMap.keys(),new MyComparator())
,其中MyComparator会比较您要排序的两个值
编写一个比较器,对两个值进行排序,例如:
public class MyComparator extends Comparator<Testing> {
public int compare(Testing a, Testing b) {
if(a.position < b. position)
return -1;
else if(a.position > b.position)
return 1;
else { //position is equal, sort on value
if(a.value < b.value)
return -1;
else if(a.value > b.value)
return 1;
}
return 0;
}
}
}
公共类MyComparator扩展比较器{
公共整数比较(测试a、测试b){
如果(a位置b位置)
返回1;
否则{//位置相等,按值排序
如果(a值b值)
返回1;
}
返回0;
}
}
}
您应该在自定义比较器中使用TreeMap,而不是HashMap,如下所示:
public enum Testing {
EXAMPLE_1(1, 2),
EXAMPLE_2(2, 100),
EXAMPLE_3(2, 20);
public int position;
public int value;
Testing(final int pos, final int value) {
this.position = pos;
this.value = value;
}
static class EnumCompare implements Comparator<Testing> {
@Override
public int compare(Testing arg0, Testing arg1) {
return arg0.value == arg1.value ? 0 : arg0.value < arg1.value ? 1 : -1;
}
}
// Testing
public static void main(String[] args) {
Map<Testing, Integer> xx = new TreeMap<Testing, Integer>(new EnumCompare());
xx.put(EXAMPLE_1, 1111);
xx.put(EXAMPLE_2, 1111);
xx.put(EXAMPLE_3, 1111);
// prints: {EXAMPLE_2=1111, EXAMPLE_3=1111, EXAMPLE_1=1111}
System.out.println(xx);
}
}
公共枚举测试{
示例_1(1,2),
示例2(2100),
例3(2,20);
公共职位;
公共价值观;
测试(最终int位置、最终int值){
这个位置=位置;
这个值=值;
}
静态类EnumCompare实现了Comparator{
@凌驾
公共整数比较(测试arg0、测试arg1){
返回arg0.value==arg1.value?0:arg0.value
但这里有点奇怪,当我以前使用树形图时,一些数据消失了,比方说我在其中添加了3个,只添加了2个。@Nature:根据比较器,可能你有两个相等的值?如果是这样-第一个被第二个覆盖。。。同样的事情也发生在HashMap中,如果两个对象彼此相等()
,这就是我得到的结果,但它似乎没有修正我认为不可能的值,因为结果是1,3,2,4,5。当我倒过来的时候,它是5,4,2,3,1。它应该是4、5、3、2、1如果希望顺序在位置部分下降,在值部分上升,则相应地调整
。这一个比较了位置和值的升序。你能描述一下你想要排序的值吗?您有一个)由2个值(位置和值)组成的枚举。B) 一个整数,它是映射中的值部分,枚举映射到该整数。您想按什么顺序排序?A)根据降序中的位置,一旦降序中的位置被排序,对象位置的位置必须固定,然后只按升序对对象的值进行排序。至于“B”,它是映射中的整数,将跟随键的最常用位置,但是,如果我洗牌hashmap,并且没有按顺序排列,它会返回我{EXAMPLE_5=4444,EXAMPLE_2=222,EXAMPLE_3=11133331,EXAMPLE_4=4444,EXAMPLE_1=1111}而不是4,5,3,2,1}你还在使用hashmap吗?我不是。你的ex仍然给了我{EXAMPLE_5=4444,EXAMPLE_2=222,EXAMPLE_3=11133331,EXAMPLE_4=4444,EXAMPLE_1=1111}
我使用了树形图,就像你上面展示的那样,但是基本上,一旦对位置进行排序,它应该是固定的,然后只对其中的值进行排序,但当我使用你的方法时,它只是对位置进行排序,然后当它对值排序时,位置又被弄乱了。我看到你的代码,它和我的代码非常不同。1.看看我的比较仪。2.你为什么要使用LinkedHashMap,你的最终地图可以是树形图,就像我在代码中显示的那样。3.不确定什么是statups
。statups是包含测试的enumMap.EXAMPLE_1,依此类推。但是当我使用树形图作为最后一个图时,主要的位置将再次被洗牌。哦,还有,你的
public enum Testing {
EXAMPLE_1(1, 2),
EXAMPLE_2(2, 100),
EXAMPLE_3(2, 20);
public int position;
public int value;
Testing(final int pos, final int value) {
this.position = pos;
this.value = value;
}
static class EnumCompare implements Comparator<Testing> {
@Override
public int compare(Testing arg0, Testing arg1) {
return arg0.value == arg1.value ? 0 : arg0.value < arg1.value ? 1 : -1;
}
}
// Testing
public static void main(String[] args) {
Map<Testing, Integer> xx = new TreeMap<Testing, Integer>(new EnumCompare());
xx.put(EXAMPLE_1, 1111);
xx.put(EXAMPLE_2, 1111);
xx.put(EXAMPLE_3, 1111);
// prints: {EXAMPLE_2=1111, EXAMPLE_3=1111, EXAMPLE_1=1111}
System.out.println(xx);
}
}