This is not a solution, but something that can be used for deriving one from it
Due to that you haven't stated what the output of the console was, I'm not able to know which specific errors you have.
Nevertheless, I created the following code for giving myself an idea:
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;
}
}
It turns out that the compiler acts as if there weren't such a thing as a Doctor object being the value of an item's ID (that acts as a key). Nevertheless, it can be seen that when one tries to print out the location in memory of each one of the Doctor items of the ArrayList passed to the getDoctorHash() function in its definition, there are no problems at all.
I don't have the slightest idea about what the reason behind of this is.
But, if instead of using the Doctor objects as values we use one of the Strings that can be obtained by making use of one of its methods, everything turns out well:
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;
}
}
HashMapdefines a mapping, e.g. in your example you would query with aStringand get aDoctorin return. This is whyput(K key, V value)needs two parameters. Maybe you want to use aHashSet?Key, which in this case you've specified to beString. So you need aStringvalue which associates to a specificDoctor(akaput("Bob", /* some doctor */);would allow usingget("Bob")to retrieve that instance back.getFirstName()). Also, I would recommend usingidas a key, as IDs are usually more unique than a first name. Otherwise, your question lacks a clear problem statement. If you don't tell us what you are having problems with, we cannot help you.