HashMaps的HashMap没有按预期填充

时间:2014-10-16 21:29:10

标签: java hash

我正在尝试从数据库下载数据并填充字符串值哈希映射的哈希映射的哈希映射的哈希映射。我希望每个键都是唯一的(它是唯一的),并且在使用put方法时只分配给它所关联的键。这是正在崩溃的部分。它分配了多个重复键,即使没有与该特定键相关联,最终值也总是覆盖以前的值。

<pre><code>
    Map<String, String> attribs = new HashMap<String, String>();
    Map<String, Map<String, String>> altKey = new HashMap<String, Map<String, String>>();
    Map<Integer, Map<String, Map<String, String>>> posKey = new HashMap<Integer, Map<String,  Map<String, String>>>();
    Map<String, Map<Integer, Map<String, Map<String, String>>>> variantData = new HashMap<String, Map<Integer, Map<String, Map<String, String>>>>();

    //skipping the database code here...

    while (rs.next()) {

                    attribs.put("ref", rs.getString(3));
                    attribs.put("genotype", rs.getString(5));
                    altKey.put(rs.getString(4), attribs);
                    posKey.put(rs.getInt(2), altKey);
                    variantData.put(rs.getString(1), posKey);
                    System.out.println(rs.getString(1) + "\t" + rs.getString(1)
                            + "\t" + rs.getInt(2) + "\t" + rs.getString(4) + "\t"
                            + rs.getString(3) + "\t" + rs.getString(5));

                }
                System.out.println("\n" + "------------------------" + "\n");

                Iterator<String> keySetIterator = variantData.keySet().iterator();

                while (keySetIterator.hasNext()) {
                    String key = keySetIterator.next();
                    Iterator<Integer> posSetIterator = variantData.get(key)
                            .keySet().iterator();
                    while (posSetIterator.hasNext()) {
                        Integer pos = posSetIterator.next();
                        Iterator<String> altSetIterator = variantData.get(key)
                                .get(pos).keySet().iterator();
                        while (altSetIterator.hasNext()) {
                            String alt = altSetIterator.next();
                            System.out.println("key 1: "
                                    + key
                                    + " key 2: "
                                    + pos
                                    + " key 3: "
                                    + alt
                                    + " values: "
                                    + variantData.get(key).get(pos).get(alt)
                                            .values());

                        }

                    }

                }
</code></pre>

<pre>

This produces the following output:

The raw output from the database:

<pre>
chr4    78962253    C   CT                      1|0
chr4    38391318    G   GAAGGGACCAGTCAAAGGGT    1|0
chr4    169518074   A   ATAGCACTTTTACCCT        1|0
chr4    3756414     A   AC                      1|0
chr4    130827032   T   TA                      0|1</pre>

------------------------

And the results of iterating over the structure:


key 1: chr4 key 2: 38391318 key 3: T values: [TA, 0|1]
key 1: chr4 key 2: 38391318 key 3: G values: [TA, 0|1]
key 1: chr4 key 2: 38391318 key 3: A values: [TA, 0|1]
key 1: chr4 key 2: 38391318 key 3: C values: [TA, 0|1]
key 1: chr4 key 2: 3756414 key 3: T values: [TA, 0|1]
key 1: chr4 key 2: 3756414 key 3: G values: [TA, 0|1]
key 1: chr4 key 2: 3756414 key 3: A values: [TA, 0|1]
key 1: chr4 key 2: 3756414 key 3: C values: [TA, 0|1]
key 1: chr4 key 2: 130827032 key 3: T values: [TA, 0|1]
key 1: chr4 key 2: 130827032 key 3: G values: [TA, 0|1]
key 1: chr4 key 2: 130827032 key 3: A values: [TA, 0|1]
key 1: chr4 key 2: 130827032 key 3: C values: [TA, 0|1]
key 1: chr4 key 2: 78962253 key 3: T values: [TA, 0|1]
key 1: chr4 key 2: 78962253 key 3: G values: [TA, 0|1]
key 1: chr4 key 2: 78962253 key 3: A values: [TA, 0|1]
key 1: chr4 key 2: 78962253 key 3: C values: [TA, 0|1]
key 1: chr4 key 2: 169518074 key 3: T values: [TA, 0|1]
key 1: chr4 key 2: 169518074 key 3: G values: [TA, 0|1]
key 1: chr4 key 2: 169518074 key 3: A values: [TA, 0|1]
key 1: chr4 key 2: 169518074 key 3: C values: [TA, 0|1]


My expected output would be something like this:


key 1: chr4 key 2: 38391318 key 3: G values: [GAAGGGACCAGTCAAAGGGT, 1|0]
key 1: chr4 key 2: 3756414 key 3: A values: [AC, 1|0]
key 1: chr4 key 2: 130827032 key 3: T values: [TA, 0|1]
key 1: chr4 key 2: 78962253 key 3: C values: [CT, 1|0]
key 1: chr4 key 2: 169518074 key 3: A values: [ATAGCACTTTTACCCT, 1|0]

Can anyone spot what I am doing wrong or am I simply using the wrong collection type for this problem.

Thanks,
</pre>

0 个答案:

没有答案