2016-04-04 11 views
2

私は、HashMapのキーと値エントリに値を設定する必要があるJavaコードシナリオを持っています。 私が使用する必要があるのIdのキーは、事前に定義されていると私は、コードのロジック処理を経て、このjava HashMapメソッド名へのキーIDのマッピング

Key=16 value= *result of method calculateZipWeather()* 
Key=23 value= *result of method calculateZipCensus()* 
key=37 value = *result of method calulateZipCrime()* 

のようなものを生成し、対応する値今日はこれを行う方法は、私がしたかった。この

String zipweather = calculateZipWeather() 
mapObj.put(16,zipweather) 

のようなものですキーIDとそれに対応するメソッド名の間に静的な外部マッピングを維持する方法があるかどうかを確認してください。

あなたの考えを教えてください。

public class Operations { 

    public String calculateZipWeather() { 
     return "Zip Weather"; 
    } 

    public String calculateZipCensus() { 
     return "Zip Census"; 
    } 

    public String calculateZipCrime() { 
     return "Zip Crime"; 
    } 

} 

次に、あなたはあなたが必要なものを行う方法です以下の(方法:あなたがすべての「計算」メソッドを含むクラスを以下したとし

+2

リフレクションを使用して、キー値をメソッド名(または 'Method'オブジェクト)にマップします。 – Thomas

答えて

0

これは、リフレクションを使用してそれを行うことができる方法でありますjava.lang.reflect.Method):

public class Populate { 

    //This map contains mapping of your 'keys' with 'methods' which need to be invoked for those keys 
    private static Map<Integer, Method> keyOperationMapping; 

    //static initializer block to initialize the above map 
    static { 
     keyOperationMapping = new HashMap<Integer, Method>(); 

     try { 
      //Getting the 'Method' object with method name as 'calculateZipWeather' from 'Operations' class and mapping it with key 
      Method method16 = Operations.class.getDeclaredMethod("calculateZipWeather"); 
      keyOperationMapping.put(16, method16); 
     } 
     catch (NoSuchMethodException e) { 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      e.printStackTrace(); 
     } 

     try { 
      //Getting the 'Method' object with method name as 'calculateZipCensus' from 'Operations' class. 
      Method method23 = Operations.class.getDeclaredMethod("calculateZipCensus"); 
      keyOperationMapping.put(16, method23); 
     } 
     catch (NoSuchMethodException e) { 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      e.printStackTrace(); 
     } 

     //Similary add more key-value pairs 

    } 

    public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { 
     //You need an Operations object on which the methods will be called/invoked 
     Operations operations = new Operations(); 

     //Map to store your results 
     Map<Integer, String> calculatedZips = new HashMap<Integer, String>(); 
     //Iterate over all key-value pairs of key with methods 
     for(Map.Entry<Integer, Method> entry : keyOperationMapping.entrySet()) { 
      //Get method to be invoked 
      Method methodToInvoke = entry.getValue(); 

      //Invoke the method on operations object, you can also pass arguments here if your method requires 
      String zipValue = (String) methodToInvoke.invoke(operations); 

      //Get key which will be your argum 
      int zipKey = entry.getKey(); 

      calculatedZips.put(zipKey, zipValue); 
     } 

    } 

} 
関連する問題