java—将唯一值插入哈希Map

ep6jt1vc  于 2021-06-29  发布在  Java
关注(0)|答案(3)|浏览(392)

我正在尝试将某个类的列表插入哈希Map。键应该是列表中的一个字段,在我的例子中是id,哈希Map的值应该是以键作为其id的列表项。
下面是我试图做的一个例子:

// example class

public class Dog{
  int id,
  int name,
  int foodEaten
}

// getters

// dog objects
Dog dog1 = new Dog(1,"Dog1", "Cheese")
Dog dog2 = new Dog(1,"Dog1", "Meat")

Dog dog3 = new Dog(2,"Dog2", "Fish")
Dog dog4 = new Dog(2,"Dog2", "Milk")

List<Dog> dogList = new ArrayList<>();

//insert dog objects into dog list

//Creating HashMap that will have id as the key and dog objects as the values
HashMap<Integer, List<Dog>> map = new HashMap<>();

这就是我想做的

for (int i = 0; i < dogList.size()-1; i++) {
  List<Dog> cx = new ArrayList<>();
  if (dog.get(i).getId() == dog.get(i+1).getId()){
    cx.add(dog.get(i));
  }
   map.put(dog.get(i).getId(), cx);
}

然而,我得到的结果是:

{
  "1": [
    {
      "id": 1,
      "name": "Dog1",
      "foodEaten": "Cheese"
    }
  ],
  "2": []
}

但这正是我想要实现的:

{
  "1": [
    {
      "id": 1,
      "name": "Dog1",
      "foodEaten": "Cheese"
    },
    {
      "id": 1,
      "name": "Dog1",
      "foodEaten": "Meat"
    }
  ],
  "2": [
    {
      "id": 2,
      "name": "Dog2",
      "foodEaten": "Fish"
    },
    {
      "id": 2,
      "name": "Dog2",
      "foodEaten": "Milk"
    }
  ]
}
bttbmeg0

bttbmeg01#

嗨,托什,欢迎来到stackoverflow。
当您检查两个对象的id时,问题出现在if语句中。

for (Dog dog : dogList) {
  if(dog.getId() == dog.getId()){
     crMap.put(cr.getIndividualId(), clientReceivables.);
  }
}

在这里,您检查同一个名为“dog”的对象的id,在if语句中您将始终得到true。这就是为什么它用两个id的所有值填充Map。
另一个问题是dog4的id等于1,即dog1和dog2的id。考虑到这一点,你仍然无法实现你想要的,所以也要检查一下。
现在是解决方案。如果你想把每只狗和下一只狗进行比较,你就需要写下不同的。我不确定你是在使用java还是android,但是有一个解决方案可以解决这个问题,而且代码的版本更干净。
有了Java8,你就有了流api,它可以帮助你做到这一点。在这里检查
同样对于android,你可以在这里查看android linq

rjjhvcjd

rjjhvcjd2#

Dog dog1 = new Dog(1, "Dog1", "Cheese");
Dog dog2 = new Dog(1, "Dog1", "Meat");

Dog dog3 = new Dog(2, "Dog2", "Fish");
Dog dog4 = new Dog(2, "Dog2", "Milk");

List<Dog> dogList = List.of(dog1, dog2, dog3, dog4);
//insert dog objects into dog list

//Creating HashMap that will have id as the key and dog objects as the values
Map<Integer, List<Dog>> map = new HashMap<>();

for (Dog dog : dogList) {
    map.computeIfAbsent(dog.id, k -> new ArrayList<>())
            .add(dog);
}

map.entrySet().forEach(System.out::println);

印刷品

1=[{1, Dog1, Cheese, {1, Dog1, Meat]
2=[{2, Dog2, Fish, {2, Dog2, Milk]

狗类

static class Dog {

    int id;
    String name;
    String foodEaten;

    public Dog(int id, String name, String foodEaten) {
        this.id = id;
        this.name = name;
        this.foodEaten = foodEaten;
    }

    public String toString() {
        return String.format("{%s, %s, %s", id, name, foodEaten);
    }

}
5n0oy7gb

5n0oy7gb3#

你可以用 Collectors.groupingBy(Dog::getId) 用同样的方法把狗分组 id .
演示:

public class Main {
    public static void main(String[] args) {
        List<Dog> list = List.of(new Dog(1, "Dog1", "Cheese"), new Dog(1, "Dog1", "Meat"), new Dog(2, "Dog2", "Fish"),
                new Dog(2, "Dog2", "Milk"));

        Map<Integer, List<Dog>> map = list.stream().collect(Collectors.groupingBy(Dog::getId));

        System.out.println(map);
    }
}

输出:

{1=[Dog [id=1, name=Dog1, foodEaten=Cheese], Dog [id=1, name=Dog1, foodEaten=Meat]], 2=[Dog [id=2, name=Dog2, foodEaten=Fish], Dog [id=2, name=Dog2, foodEaten=Milk]]}

这个 toString 实施:

public String toString() {
    return "Dog [id=" + id + ", name=" + name + ", foodEaten=" + foodEaten + "]";
}

相关问题