2013-08-23 60 views
1

我有一個帶有嵌套散列的java程序。當我在最內部的嵌套中調用一個值時,我得到一個錯誤,指出我不能爲類型對象調用get,但是當我調用getClass()。getName()時,我得到了HashMap。這裏是回溯Java hashmap將不允許調用get方法

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

回答

4

您已經聲明

HashMap<String, HashMap> analysisHash; 

其中內HashMap沒有類型參數。所以它默認爲Object

Object類型的對象調用get(String)

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; 

或其他任何你需要的。

+0

有沒有一種方法來轉換泛型HashMap,而不必最初做呢? – user1876508

+0

@ user1876508這是可能的,但你將擁有所有這些嵌套的轉換。至少在多行上做更清晰的代碼。 –

+0

你知道任何有關java的優秀風格指南嗎?我不知道如何將我的150個字符長的行變成適合閱讀的東西。 – user1876508

相關問題