如何通过属性订购HashMap

问题描述 投票:0回答:1

正如您可能已经知道的那样,当您将某些内容放入HashMap中时,其存储顺序是随机的。我想使用Comparable订购我的HashMap,但无法使其正常工作。我希望有一个人可以帮助我。 所以我有一张地图:Map<MyKeyObject, List<MyValueObject>> myObjectMap = new HashMap<>();该映射的键是一个由多个ID和一个名称(MyKeyObject)构成的构造,我想首先基于ID对键上的映射进行排序,如果ID相同,则命名。 这是我尝试过的:

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方法被击中了……我在这里做错了吗?

java sorting hashmap comparable
1个回答
0
投票

我认为您正在寻找的是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);
        }

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.