2

3 つの値を含むリスト/マップに値を格納する必要があります。

size  |   amount |  description
2,34  |     4    |  "I'm a long String"
1,14  |     2    |  "I'm another long String"

そこで、次のようなネストされた Map を使用することを考えました。

        Map<Double, Map<Integer,String>> a = new HashMap<Double, Map<Integer,String>>();

しかし、どうすればそのマップにエントリを簡単に追加できますか? 構文のような

a.put(1,43, new Map.Entry<>(7,"9890"));

うまくいきませんでした。原因Mapは抽象的です。ネストされたマップを取得する最良の方法は何ですか?

4

3 に答える 3

1

Entry クラスを定義するだけです。

final class Entry {
    final int amount;
    final String description;

    Entry(final int amount, final String description) {
        // check parameters
        this.amount = amount;
        this.description = description;
    }


    // implement getAmount
    // implement getDescription
}

マップのタイプは

Map<Double, Entry>
于 2013-05-13T17:33:46.280 に答える