簡體   English   中英

如何從對象列表中返回HashMap?

[英]How to return a HashMap from a list of objects?

目前,我正在嘗試返回HashMap 用的參數ArrayList具有50級或更多的項。

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

我將Id作為鍵,將Doctor對象作為值。

我的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.
}

不知道為什么要這樣做(可以使用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;
}

然后,您必須為Doctor d = doctorMap.get("firstname")獲得一些firstname

這不是解決方案,而是可用於從中推導解決方案的方法

由於您尚未說明控制台的輸出,因此我無法知道您有哪些特定錯誤。

盡管如此,我還是創建了以下代碼來給自己一個主意:

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

}

事實證明,編譯器的行為就好像沒有Doctor對象是項ID(用作鍵)的值一樣。 但是,可以看出,當嘗試打印出定義中傳遞給getDoctorHash()函數的ArrayList的每個Doctor項在內存中的位置時,根本沒有問題。 我對這背后的原因一無所知。

但是,如果不是使用Doctor對象作為值,而是使用可以通過使用其方法之一獲得的String之一,那么一切都很好:

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM