Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 哈希映射和操作_Java_Sorting_Hashmap - Fatal编程技术网

Java 哈希映射和操作

Java 哈希映射和操作,java,sorting,hashmap,Java,Sorting,Hashmap,我对HashMap有问题。 我想在地图上写下: key - 320, value - 0.1 key - 321, value - 0.7 key - 322, value - 0.5 key - 323, value - 0.6 之后,我想按如下降序排列此地图: key - 321, value - 0.7 key - 323, value - 0.6 key - 322, value - 0.5 key - 320, value - 0.1 之后,我想按升序对映射进行排序,但只对值进行排

我对HashMap有问题。 我想在地图上写下:

key - 320, value - 0.1
key - 321, value - 0.7
key - 322, value - 0.5
key - 323, value - 0.6
之后,我想按如下降序排列此地图:

key - 321, value - 0.7
key - 323, value - 0.6
key - 322, value - 0.5
key - 320, value - 0.1
之后,我想按升序对映射进行排序,但只对值进行排序:

key - 321, value - 0.1
key - 323, value - 0.5
key - 322, value - 0.6
key - 320, value - 0.7

这在hashmaps中是可能的吗?如何做到这一点?

不,不能使用HashMap或任何标准jdk map实现按值排序。

HashMap不维护顺序,因此无法排序。您应该在对象中放置键和值

class Pair {
    int key;
    doulbe value;
}
将它们放在ArrayList中,并使用Collections.sort()对它们进行排序

class Pair {
    int key;
    double value;

    public Pair(int key, double value) {
        this.key = key;
        this.value = value;
    }
}

class Desc implements Comparator<Pair> {
    @Override
    public int compare(Pair o1, Pair o2) {
        return (int) Math.signum(o1.value - o2.value);
    }
}

class Asc implements Comparator<Pair> {
    @Override
    public int compare(Pair o1, Pair o2) {
        return (int) Math.signum(o2.value - o1.value);
    }
}

public class Test {
    public static void dump(Collection<Pair> col) {
        for ( Pair p :col ) {
            System.out.println("key = " + p.key + " value = " + p.value );
        }
    }
    public static void main(String[] args) {
        ArrayList<Pair> p = new ArrayList<Pair>();
        for (int i = 320; i < 325; i++) {
            p.add(new Pair(i, (double) i / 1000));
        }
        Collections.sort(p, new Asc());
        dump(p);
        System.out.println("------------------------");
        Collections.sort(p, new Desc());
        dump(p);
    }

}
类对{
int键;
双重价值;
公共对(int键,双值){
this.key=key;
这个值=值;
}
}
类Desc实现了比较器{
@凌驾
公共整数比较(对o1,对o2){
返回(int)数学符号(o1.value-o2.value);
}
}
类Asc实现比较器{
@凌驾
公共整数比较(对o1,对o2){
返回(int)数学符号(o2.value-o1.value);
}
}
公开课考试{
公共静态无效转储(集合列){
用于(对p:col){
System.out.println(“key=“+p.key+”value=“+p.value”);
}
}
公共静态void main(字符串[]args){
ArrayList p=新的ArrayList();
对于(int i=320;i<325;i++){
p、 增加(新的一对(一对,(双)一对/1000);
}
Collections.sort(p,new Asc());
垃圾场(p);
System.out.println(“---------------------------”);
Collections.sort(p,newdesc());
垃圾场(p);
}
}

否。HashMap是Map insteface的一个实现,它不保留任何键顺序。它根据密钥的散列码放置密钥


但你有更简单的解决办法。您应该改用TreeMap。这个类接受比较器。将它与您自己的比较器一起使用,您甚至不必对任何内容进行排序。按键将根据比较器返回。要反转顺序,您可以使用其他树映射,也可以只说
newarraylist(t.keySet())
,然后反转列表的顺序。

是否确实要在第三步中更改从键到值的映射?我需要更改此值,因为在下一步中我将执行一些操作。下一步我必须乘以321*0.1、323*0.5等,除非我可以在不改变映射的情况下这样做?