簡體   English   中英

如何在 Java 的 map 中插入三項?

[英]How to insert triple items in a map in Java?

因此,主要思想是創建例如:

Map<Map<String, String>, Integer> map = new LinkedHashMap<>();

我需要放入這個 map 例如項目:

{{France=Paris}=12, {Spain=Madrid}=2, ... }

這個怎么做? 如何實現“放”function? map.put("France","Paris",2)不起作用。

您需要首先創建一個Map (例如Map.of("France", "Paris") )並將其放入更大的Map中。

執行以下操作:

import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<Map<String, String>, Integer> map = new LinkedHashMap<>();
        map.put(Map.of("France", "Paris"), 2);
        System.out.println(map);
    }
}

Output:

{{France=Paris}=2}

或者,

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<Map<String, String>, Integer> map = new LinkedHashMap<>();
        Map<String, String> data = new HashMap<>();
        data.put("France", "Paris");
        map.put(data, 2);
        System.out.println(map);
    }
}

注意:正如 Mureinik 已經提到的,在map 中將可變對象作為鍵(替代解決方案)很容易出錯。 您可以查看此答案以獲取解釋。

盡管 Arvind Kumar Avinash 的回答完美地解決了這個問題,但請注意,您應該重新考慮您的數據結構。

不建議將Map作為另一個Map的密鑰。 您應該制作另一個 class 來作為Map的密鑰。 如果您對將大寫作為鍵感興趣,請考慮實現一個不可變的 class ,例如這個,其中equals()hashcode()被正確覆蓋:

public class Capital {

    private final String country;
    private final String name;

    public Capital(String country, String name) {
        this.country = country;
        this.name = name;
    }

    public boolean equals(final Object o) {
        if (o == this) return true;
        if (!(o instanceof Capital)) return false;
        final Capital other = (Capital) o;
        if (!other.canEqual((Object) this)) return false;
        final Object this$country = this.country;
        final Object other$country = other.country;
        if (this$country == null ? other$country != null : !this$country.equals(other$country)) return false;
        final Object this$name = this.name;
        final Object other$name = other.name;
        if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
        return true;
    }

    protected boolean canEqual(final Object other) {
        return other instanceof Capital;
    }

    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final Object $country = this.country;
        result = result * PRIME + ($country == null ? 43 : $country.hashCode());
        final Object $name = this.name;
        result = result * PRIME + ($name == null ? 43 : $name.hashCode());
        return result;
    }

    public String toString() {
        return "Capital(country=" + this.country + ", name=" + this.name + ")";
    }
}

如果您想擺脫樣板代碼,請考慮使用Lombok Project 上面列出的 class 是以下代碼的 deomboked 版本:

@ToString
@EqualsAndHashCode
@RequiredArgsConstructor
public class Capital {

    private final String country;
    private final String name;
    
}

霍爾格評論后更新:

除了上述的 delomboked 版本之外, equals()hashCode()的其他實現也是可能的,但在代碼風格和性能等某些標准上有所不同。 和 Holger 一樣,我認為 delomboked 的實現不應該是手工制作的。 它只是通過使用Lombok @EqualsAndHashCode注釋而產生的實現。

為了正確覆蓋hashCode()equals()需要滿足一些要求:

盡管性能和良好的代碼樣式很重要,但它們不在這些要求之列。 在我看來,使用 Lombok,代碼風格不太重要,因為代碼是生成的。 在開發過程中通常看不到它。

canEqual()方法存在,因為 Lombok 也生成它。

由於您使用一對字符串作為鍵,您可以使用Map.Entry作為 map 的鍵,而不是使用Map<String, String>

Map<Map.Entry<String, String>, Integer> map = new LinkedHashMap<>();
map.put(Map.entry("France", "Paris"), 12);
map.put(Map.entry("Spain", "Madrid"), 2);
System.out.println(map.get(Map.entry("France", "Paris")));

Output: 12

暫無
暫無

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

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