java - 如何按属性对 HashMap 进行排序
问题描述
您可能已经知道,当您将某些内容放入 HashMap 时,存储内容的顺序是随机的。我想使用 Comparable 订购我的 HashMap,但我无法让它正常工作。
所以我有一张地图:
Map<MyKeyObject, List<MyValueObject>> myObjectMap = new HashMap<>();
这个map的key是多个id和name(MyKeyObject)的构造,我想先根据id对key上的map进行排序,如果id相同则name。
这是我尝试过的:
public class MyKeyObject implements Comparable<MyKeyObject> {
private Long id;
private String name;
public MyKeyObject(Long id, String name) {
this.id = id;
this.name = name;
}
public boolean equals(Long id, String name) {
return this.id.equals(id) && this.name.equals(name)
}
@Override
public int compareTo(MyKeyObject myKeyObject) {
if (this.id myKeyObject.getId() != 0) {
return (this.Id - myKeyObject.getId() == 1) ? 1 : -1;
} else {
return (this.name().compareTo(myKeyObject.name()) == 1) ? 1 : -1;
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyKeyObject that = (MyKeyObject) o;
return id.equals(that.id) &&
name == that.name;
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
}
public class MyKeyObject implements Comparable<MyKeyObject> {
Map<MyKeyObject, List<MyValueObject>> myObjectMap = new HashMap<>();
//Here I have a lot of code that populates the HashMap
myObjectMap.entrySet().stream().sorted(Map.Entry.comparingByKey());
}
老实说,我什至不认为 compareTo 方法被击中,我在这里做错了什么?
更新:我知道有诸如 TreeMap 之类的类型,但它对我不起作用。我刚刚在这里给出了一个非常简单的例子,我的真实代码非常复杂。是否可以像我在这里尝试过的那样使用 Comparable 进行这项工作?
解决方案
我认为您正在寻找的是TreeMap而不是 HashMap。
在这里,您有一个用例的简化示例。
import java.util.Comparator;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
public class TreeMapExample {
public static void main(String[] args) {
Map<MyKeyObject, Object> myTreeMap = new TreeMap<>();
myTreeMap.put(new MyKeyObject(5L, "Jay"), null);
myTreeMap.put(new MyKeyObject(5L, "Bob"), null);
myTreeMap.put(new MyKeyObject(1L, "Alison"), null);
myTreeMap.put(new MyKeyObject(3L, "Frey"), null);
myTreeMap.entrySet()
.forEach(myKeyObjectObjectEntry ->
System.out.println(String.format(
"Id= %s, Name=%s",
myKeyObjectObjectEntry.getKey().id,
myKeyObjectObjectEntry.getKey().name )));
}
public static class MyKeyObject implements Comparable<MyKeyObject> {
private Long id;
private String name;
public MyKeyObject(Long id, String name) {
this.id = id;
this.name = name;
}
@Override
public int compareTo(MyKeyObject myKeyObject) {
return Comparator.comparing((MyKeyObject keyObject)->keyObject.id)
.thenComparing(keyObject->keyObject.name)
.compare(this, myKeyObject);
}
}
}
输出是:
Id=1, Name=Alison
Id=3, Name=Frey
Id=5, Name=Bob
Id=5, Name=Jay
您可以MyKeyObject
为 TreeMap 设置 Comparable 或提供 Comparator。
推荐阅读
- docker - 创建 tf.Variable 时 docker 内的 tensorflow2.1 报告 OOM
- karate - 如何测试对象数组与空手道中的模式匹配
- javascript - Javascript 使用按钮关闭切换
- python-3.x - pandas中的ndarray值可视化和覆盖
- mongodb - springData mongodb分组操作
- vue.js - 为什么输入会重置 fn1 中的光标位置而不会重置 fn2 中的光标位置?
- javascript - Discord.js 机器人禁止命令不发送消息。不和谐.js 11.6.2
- java - SpringBoot,Kafka:java.lang.NoSuchMethodError:org.apache.kafka.clients.producer.Producer.close(Ljava/time/Duration;)V
- excel - 在两张纸上匹配单独的值并打印出是或否
- python - ImportError:无法导入名称“deserialize_keras_object”