0

Currently, I am trying to return a HashMap. With a parameter of an ArrayList that has 50 or more entries.

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

I am passing the Id as the key and doctor object as the value..

My Doctor class is simple:

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.
}
7
  • change employeeList to doctorList and Doctor.size() to doctorList.size() Commented Oct 30, 2016 at 22:56
  • 1
    A HashMap defines a mapping, e.g. in your example you would query with a String and get a Doctor in return. This is why put(K key, V value) needs two parameters. Maybe you want to use a HashSet? Commented Oct 30, 2016 at 22:57
  • 4
    Well, what should be your key? The point of a Map is to associate a value with a Key, which in this case you've specified to be String. So you need a String value which associates to a specific Doctor (aka put("Bob", /* some doctor */); would allow using get("Bob") to retrieve that instance back Commented Oct 30, 2016 at 22:57
  • 6
    You need to adopt a rational process here. Your code doesn't compile. Understand why by reading the error messages, not ignoring them. Try to make sense of them. You don't know what a HashMap is and how it works, then read its documentation. The documentation is too obscure, then google for "Java HashMap tutorial", and read. Commented Oct 30, 2016 at 23:06
  • First off, your code misses some parentheses (it should be .getFirstName()). Also, I would recommend using id as 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. Commented Oct 30, 2016 at 23:11

2 Answers 2

1

Not sure why you want to do it this way, (you could use a Java8 stream and filter the list for first names), but you are close.

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

Or, more simply

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

Then, you have to Doctor d = doctorMap.get("firstname") for some firstname

Sign up to request clarification or add additional context in comments.

3 Comments

It turns out that the problem persists; when one tries to get the value of one of the created HashMap's keys, the console returns a NullPointerException error. I don't have the slightest idea of what the reason behind this could be, nevertheless.
I don't know what you mean. If you have non-null Doctor objects in the list, then nothing throws NPE.
In the first snippet of code that I implemented in my answer, inside of the definition of the getDoctorHash() function, there's a System.out.println() statement that is used for assuring that the Doctor object is not null at the beginning. In spite of this, a NPE is thrown later.
1

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.