簡體   English   中英

將唯一值插入 hash map

[英]Insert Unique values into a hash map

我正在嘗試將某個 class 的列表插入到 hash map 中。 鍵應該是列表中的字段之一,在我的例子中是 id,而 hash 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"
    }
  ]
}

您可以使用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);
    }
}

Output:

{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 + "]";
}
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]

狗class

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);
    }
    
}

嗨 Tosh,歡迎來到 Stackoverflow。

當您檢查兩個對象的 ID 時,問題出在您的 if 語句中。

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

在這里,您檢查名為“dog”的相同 object 的 ID,並且在此 if 語句中您將始終得到True 這就是為什么它會用兩個 ID 的所有值填充您的 map。

您遇到的另一個問題是您的 dog4 的 ID 等於 1,即 dog1 和 dog2 的 ID。 考慮到這一點,你仍然無法實現你想要的,所以也要檢查一下。

現在尋求解決方案。 如果你想通過列表 go 並將每只狗與下一只狗進行比較,那么你需要寫出不同的東西。 我不確定您是在使用 java 還是使用 android,但有一個解決方案可以解決這個問題,並且代碼更簡潔。

使用 Java 8 你得到了 Stream API 可以幫助你。 在這里檢查

同樣對於 android 你有 android-linq 你可以在這里查看

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM