Java hashmap不允许调用get方法

时间:2013-08-23 18:11:56

标签: java hashmap nested

我有一个嵌套哈希的java程序。当我在最内部的嵌套中调用一个值时,我会收到一个错误,指出我不能为类型对象调用get,但是当我调用getClass()。getName()时,我得到了HashMap。这是追溯的副本

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method get(String) is undefined for the type Object

    at EpitopeAnalysis.parseColumns(EpitopeAnalysis.java:88)
    at EpitopeAnalysis.<init>(EpitopeAnalysis.java:43)
    at JobManager.<init>(JobManager.java:42)
    at Analyzer.main(Analyzer.java:13)

以下是我的代码

的副本
import java.awt.List;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import org.bson.types.BasicBSONList;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;


public class EpitopeAnalysis {
    int combinations;
    int[] positions;
    String locus;
    ArrayList<Subject> subjects;

    private static String[] ValidAcids = {
            "A","B","C","D","E","F","G","H","I","K","L","M",
            "N","P","Q","R","S","T","U","V","W","Y","Z"
    };

    @SuppressWarnings("unchecked")
    EpitopeAnalysis(BasicDBObject jobObject) {
        combinations = (int) jobObject.get("combinations");
        locus = (String) jobObject.get("locus");

        // Create subject ArrayList
        Gson gson = new Gson();
        Type subjectType = new TypeToken<ArrayList<Subject>>(){}.getType();
        subjects = gson.fromJson(jobObject.get("subjects").toString(), subjectType);

        // Create an array of positions
        BasicDBList _pos = (BasicDBList) jobObject.get("positions");
        positions = new int[_pos.size()];
        for (int i = 0; i < _pos.size(); i++) {
            positions[i] = (int)_pos.get(i);
        }

        parseColumns();
    }

    private void parseColumns() {
        ArrayList<String> affectedAlleles = new ArrayList<String>();
        ArrayList<String> controlledAlleles = new ArrayList<String>();

        // Segregate alleles by the dx of the subjects
        for(int i = 0; i < subjects.size(); i++) {
            if (subjects.get(i).dx.toUpperCase().equals("AFFECTED")) {
                affectedAlleles.add(subjects.get(i).alleles[0]);
                affectedAlleles.add(subjects.get(i).alleles[1]);
            }
            else if (subjects.get(i).dx.toUpperCase().equals("CONTROL")) {
                controlledAlleles.add(subjects.get(i).alleles[0]);
                controlledAlleles.add(subjects.get(i).alleles[1]);
            }
        }

        //System.out.println(affectedAlleles[4]);
        /* 
         * Generate schema for analysisHash
         *  {
         *      "AFFECTED": {
         *          1: {"A": 0, "B": 0, ...},
         *          2: {},
         *      }
         *      "CONTROL": { ... }
         *  }
         * */

        HashMap<String, HashMap> analysisHash = new HashMap<String, HashMap>();
        analysisHash.put("AFFECTED", new HashMap<Integer, HashMap>());
        analysisHash.put("CONTROL", new HashMap<Integer, HashMap>());
        for(int i = 0; i < positions.length; i++) {
            analysisHash.get("AFFECTED").put(
                        positions[i], 
                        generateAcidHash()
                    );
            analysisHash.get("CONTROL").put(
                        positions[i], 
                        generateAcidHash()
                    );
        }

        /*
         *  Iterate over positions
         *      Iterate over alleles
         *      append to analysisHash
         * */
            // returns java.util.HashMap
            System.out.println(
                analysisHash.get("AFFECTED").get(9).getClass().getName()
            );

            // I am given the error here
        System.out.println(analysisHash.get("AFFECTED").get(9).get("A"));

    private HashMap<String, Integer> generateAcidHash() {
        HashMap<String, Integer> acidHash = new HashMap<String, Integer>();

        for(String acid: ValidAcids) {
            acidHash.put(acid, 0);
        }

        return acidHash;
    }
}

1 个答案:

答案 0 :(得分:4)

你已宣布

 HashMap<String, HashMap> analysisHash;

其中内部HashMap没有类型参数。因此默认为Object

您正在get(String)类型的对象上调用Object

System.out.println(analysisHash.get("AFFECTED").get(9).get("A"));
                                                  |     |
                                                  |     |---------->is called on instance of type Object
                                                  | ---------------> returns an instance of type Object

Object没有get(String)方法。

将声明更改为

HashMap<String, HashMap<Integer, HashMap<String, SomeType>>> analysisHash;

或其他任何你需要的东西。