Apex Mapのgetメソッドの使い方のサンプル

環境
Salesforce

構文
Map<String,Integer>変数名 = new Map<String,Integer>();
変数名.put(キー,値);

変数名.get(キー)
get メソッドでそのキーの値が返されます。
提供されたキーが値に対応付けられていない場合は、get メソッドで null が返されます。

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class CityInfo {
public static Integer stuAge() {
Map<String,Integer>stu = new Map<String,Integer>();
stu.put('yamada',21);
stu.put('oosaki',31);
stu.put('motohashi',41);
Integer age = stu.get('motohashi');
System.debug('years old: '+age);
return age;
}
}
public class CityInfo { public static Integer stuAge() { Map<String,Integer>stu = new Map<String,Integer>(); stu.put('yamada',21); stu.put('oosaki',31); stu.put('motohashi',41); Integer age = stu.get('motohashi'); System.debug('years old: '+age); return age; } }
public class CityInfo {
    public static Integer stuAge() {
        Map<String,Integer>stu = new Map<String,Integer>();
        stu.put('yamada',21);
        stu.put('oosaki',31);
        stu.put('motohashi',41);
        
        Integer age = stu.get('motohashi');
        System.debug('years old: '+age);
        return age;
        
    }
}

メソッド「stuAge」を実行する
CityInfo.stuAge();

結果
years old: 41

Apex

Posted by arkgame