2016-10-30 5 views
0

現在、私はHashMapを返そうとしています。 50以上のエントリを持つArrayListのパラメータを使用します。オブジェクトのリストからHashMapを返す方法は?

public static HashMap<String, Doctor> getDoctorHash(ArrayList<Doctor> doctorList) { 

    HashMap<String, Doctor> hm = new HashMap<>(); 

    for(Doctor doctor : doctorList) { 
     hm.put(doctor.getId(), doctor); 
    } 

    return hm; 
} 

私は値としてキーと医師オブジェクトとして同上を渡しています。..

Doctorクラスは単純です:

public class Doctor { 
    private String firstName, lastName, id; 

    public Doctor(String firstName, String lastName, String id) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.id = id; 
    } 
    //getter and setters etc. 
} 
+0

doctorListへの変更にEmployeeList Doctor.size()からdoctorList.size() – developer

+1

['HashMap'](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html)はマッピングを定義しますが、例えばあなたの例では 'String'を使って問い合わせを行い、' Doctor'を返します。このため、[put(K key、V value)]](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#put-KV-)には2つのパラメータが必要です。おそらく、あなたは['HashSet'](https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html)を使いたいでしょうか? – Turing85

+4

あなたの鍵は何でしょうか? Mapのポイントは、値を 'Key'に関連づけることです。この場合、' String'が指定されています。したがって、特定の 'Doctor'(別名put(" Bob "、/ * some doctor * /))に関連する' String'値が必要です。 'get(" Bob ")'を使ってそのインスタンスを取り戻すことができます – Rogue

答えて

1

ないあなたは、このようにそれをやりたい理由を確認してください、(あなたはJava8のストリームを使用し、filterファーストネームのリストを使用することができます)、あなたは近くにいます。

public static HashMap<String, Doctor> getDoctorHash(ArrayList<Doctor> doctorList) { 

    HashMap<String, Doctor> hm = new HashMap<>(); 

    for(int i = 0; i < doctorList.size(); i++) { 
     hm.put(doctorList.get(i).getFirstName(), doctorList.get(i)); 
    } 

    return hm; 
} 

あるいは、もっと単純に

public static HashMap<String, Doctor> getDoctorHash(ArrayList<Doctor> doctorList) { 

    HashMap<String, Doctor> hm = new HashMap<>(); 

    for(Doctor d : doctorList) { 
     hm.put(d.getFirstName(), d); 
    } 

    return hm; 
} 

その後、あなたはいくつかのfirstname

+0

問題は解決しません。作成されたHashMapのキーの1つの値を取得しようとすると、コンソールはNullPointerExceptionエラーを返します。それにもかかわらず、私はこの背後にある理由が何であるかということについては少しも考えていません。 – SpaceCore186

+0

あなたは何を意味するのか分かりません。リストにnull以外のDoctorオブジェクトがある場合、NPEは何もスローされません。 –

+0

私の答えで実装したコードの最初のスニペットでは、getDoctorHash()関数の定義の中に、Doctorオブジェクトが最初はnullでないことを保証するために使用されるSystem.out.println()ステートメントがあります。それにもかかわらず、後でNPEが投げられる。 – SpaceCore186

0

ためDoctor d = doctorMap.get("firstname")に持ってこれが解決策ではありませんが、それから1を導出するために使用することができるもの

あなたはコンソールの出力が何であるかを指定していないため、私はknにできませんあなたが持っている特定のエラー。項目の値であるDoctorオブジェクトのようなものがなかったかのように、コンパイラが作用すること

public class StartingPoint { 

    static String[] doctorNames = {"Potato", "Chocolate", "Something", "Name", "Unnamed"}; 

    public static void main(String[] args) {    

     ArrayList<Doctor> doctorList = new ArrayList<>(5); 

     for (int i = 0; i < 5; i++) { 
      doctorList.add(new Doctor(doctorNames[i], doctorNames[i] + " last name", String.valueOf(i))); 
     } 

     HashMap<String, Doctor> someHashMap = getDoctorHash(doctorList); 

     for (int i = 0; i < 5; i++) { 
      System.out.println("The ID of the doctor number " + String.valueOf(i + 1) + " is: "); 
      System.out.println(someHashMap.get(doctorNames[i]).getId()); 
     } 
    } 

    public static HashMap<String, Doctor> getDoctorHash(ArrayList<Doctor> doctorList) { 

     HashMap<String, Doctor> hm = new HashMap<>(); 

     for(Doctor doctor : doctorList) { 
      System.out.println(doctor); 
      hm.put(doctor.getId(), doctor); 
     } 

     return hm; 
    } 

} 

それは結局のところ:

それにもかかわらず、私は自分自身のアイデアを与えるために、次のコードを作成しましたID(キーとして機能する)。それにもかかわらず、の定義に渡されたArrayListの項目のそれぞれのメモリ内の場所をプリントアウトしようとすると、全く問題はないことがわかります。 私はこの背後にある理由が何であるかについて少しは考えていません。

しかし、代わりに私たちはそのメソッドの1つを利用することによって得ることができるString秒のいずれかを使用する値としてDoctorオブジェクトを使用するのでは、すべてがうまく判明した場合:

public class StartingPoint { 

    static String[] doctorNames = {"Potato", "Chocolate", "Something", "Name", "Unnamed"}; 


    public static void main(String[] args) { 

     ArrayList<Doctor> doctorList = new ArrayList<>(5); 

     for (int i = 0; i < 5; i++) { 
      doctorList.add(new Doctor(doctorNames[i], doctorNames[i] + " last name", String.valueOf(i))); 
     } 

     HashMap<String, String> someHashMap = getDoctorHash(doctorList); 

     for (int i = 0; i < 5; i++) { 
      System.out.println("The first name of the doctor number " + String.valueOf(i + 1) + " is: "); 
      System.out.println(someHashMap.get(String.valueOf(i))); 
     } 
    } 

    public static HashMap<String, String> getDoctorHash(ArrayList<Doctor> doctorList) { 

     HashMap<String, String> hm = new HashMap<>(); 

     for(Doctor doctor : doctorList) { 
      hm.put(doctor.getId(), doctor.getFirstName()); 
     } 

     return hm; 
    } 

} 
関連する問題