Reputation: 3494
1 hospital will have multiple doctors. and i need to know a java collection
type(ArrayList,HashMap
etc) where i could store hospital ID
and a Doctor
object in a suitable java collection
type (ArrayList,HashMap
etc).
The requirement is that i should be able to store the HospitalID
as a key
and the Doctor
object as it's value
.
Furthermore, I should be able to have the same key for various Doctor object (as there can be many doctors working for the said hospital). So what is the java
collection type (ArrayList,HashMap
etc) where i can use for this scenario ?
Note: i can't use a HashMap
- because it takes unique IDs.
Later on i should be able to filter out all the doctors that work for a particular hospital (by searching from it's ID) , and display its records
Upvotes: 1
Views: 1077
Reputation: 11
Better to use ** Map> ** instered of ** Map>**. List out all the doctors in Set insted of ArrayList. There is no priearity to follw the order right. Go with Set it will give better performace than ArrayList in Map>
Upvotes: 0
Reputation: 7016
Create a Class called Hospital.java
package com.rais.hospital;
/**
* @author Rais.Alam
* @project MyFirstProject
* @date Dec 24, 2012
*/
public class Hospital
{
private Integer hospitalId;
private String hospitalName;
private String hospitalAddress;
private Long contatNumber;
/**
* @param hospitalId
* @param hospitalName
* @param hospitalAddress
* @param contatNumber
*/
public Hospital(Integer hospitalId, String hospitalName, String hospitalAddress, Long contatNumber)
{
super();
this.hospitalId = hospitalId;
this.hospitalName = hospitalName;
this.hospitalAddress = hospitalAddress;
this.contatNumber = contatNumber;
}
/**
* @param hospitalId
* @param contatNumber
*/
public Hospital(Integer hospitalId)
{
super();
this.hospitalId = hospitalId;
}
/**
* @return the hospitalId
*/
public Integer getHospitalId()
{
return hospitalId;
}
/**
* @param hospitalId the hospitalId to set
*/
public void setHospitalId(Integer hospitalId)
{
this.hospitalId = hospitalId;
}
/**
* @return the hospitalName
*/
public String getHospitalName()
{
return hospitalName;
}
/**
* @param hospitalName the hospitalName to set
*/
public void setHospitalName(String hospitalName)
{
this.hospitalName = hospitalName;
}
/**
* @return the hospitalAddress
*/
public String getHospitalAddress()
{
return hospitalAddress;
}
/**
* @param hospitalAddress the hospitalAddress to set
*/
public void setHospitalAddress(String hospitalAddress)
{
this.hospitalAddress = hospitalAddress;
}
/**
* @return the contatNumber
*/
public Long getContatNumber()
{
return contatNumber;
}
/**
* @param contatNumber the contatNumber to set
*/
public void setContatNumber(Long contatNumber)
{
this.contatNumber = contatNumber;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((hospitalId == null) ? 0 : hospitalId.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Hospital other = (Hospital) obj;
if (!hospitalId.equals(other.hospitalId))
return false;
return true;
}
}
Create A class Doctor.java
package com.rais.hospital;
/**
* @author Rais.Alam
* @project MyFirstProject
* @date Dec 24, 2012
*/
public class Doctor
{
private Integer id;
private String name;
private String address;
private String department;
/**
* @param id
* @param name
* @param address
* @param department
*/
public Doctor(Integer id, String name, String address, String department)
{
super();
this.id = id;
this.name = name;
this.address = address;
this.department = department;
}
/**
* @return the id
*/
public Integer getId()
{
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id)
{
this.id = id;
}
/**
* @return the name
*/
public String getName()
{
return name;
}
/**
* @param name the name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return the address
*/
public String getAddress()
{
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address)
{
this.address = address;
}
/**
* @return the department
*/
public String getDepartment()
{
return department;
}
/**
* @param department the department to set
*/
public void setDepartment(String department)
{
this.department = department;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return "Doctor [id=" + id + ", name=" + name + ", address=" + address + ", department=" + department + "]";
}
}
Now access all the doctor list with the help of client described below Client.java
package com.rais.hospital;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Rais.Alam
* @project MyFirstProject
* @date Dec 24, 2012
*/
public class Client
{
private static Map<Hospital, List<Doctor>> repo = new HashMap<Hospital, List<Doctor>>();
/**
* @param args
*/
public static void main(String[] args)
{
// Displaying records for Hospital for HospitalA of Boston
createRepository();
List<Doctor> lst1 = getDoctorsList(new Hospital(101));
for (Doctor doctor : lst1)
{
System.out.println(doctor);
}
System.out.println("==================================");
// Displaying records for Hospital for HospitalB of Atlanta
List<Doctor> lst2 = getDoctorsList(new Hospital(201));
for (Doctor doctor : lst2)
{
System.out.println(doctor);
}
}
public static List<Doctor> getDoctorsList(Hospital hospital)
{
return repo.get(hospital);
}
public static void createRepository()
{
Hospital hospital1 = new Hospital(101, "HospitalA", "Street no, 101, Boston", 123456789l);
Hospital hospital2 = new Hospital(201, "HospitalB", "Street no, 102, Atlanta", 987654321l);
List<Doctor> list1 = new ArrayList<Doctor>();
List<Doctor> list2 = new ArrayList<Doctor>();
list1.add(new Doctor(1011, "Doctor-P", "Boston", "ENT"));
list1.add(new Doctor(1012, "Doctor-Q", "Boston", "ENT"));
list1.add(new Doctor(1013, "Doctor-R", "Boston", "ENT"));
list1.add(new Doctor(1014, "Doctor-S", "Boston", "ENT"));
list2.add(new Doctor(2011, "Doctor-A", "Atlanta", "Therapist"));
list2.add(new Doctor(2012, "Doctor-B", "Atlanta", "Therapist"));
list2.add(new Doctor(2013, "Doctor-C", "Atlanta", "Therapist"));
list2.add(new Doctor(2014, "Doctor-D", "Atlanta", "Therapist"));
repo.put(hospital1, list1);
repo.put(hospital2, list2);
}
}
You will see the out put as mentioned below
Doctor [id=1011, name=Doctor-P, address=Boston, department=ENT]
Doctor [id=1012, name=Doctor-Q, address=Boston, department=ENT]
Doctor [id=1013, name=Doctor-R, address=Boston, department=ENT]
Doctor [id=1014, name=Doctor-S, address=Boston, department=ENT]
==================================
Doctor [id=2011, name=Doctor-A, address=Atlanta, department=Therapist]
Doctor [id=2012, name=Doctor-B, address=Atlanta, department=Therapist]
Doctor [id=2013, name=Doctor-C, address=Atlanta, department=Therapist]
Doctor [id=2014, name=Doctor-D, address=Atlanta, department=Therapist]
Upvotes: 1
Reputation: 15644
You can have an ArrayList
of Doctor
objects and then create a HashMap
that stores the HospitalID
as the key and ArrayList
of doctors as the value:
ArrayList<Doctor> a = new ArrayList<Doctor>();
a.add(new Doctor());
// put all the doctors
HashMap<Integer,ArrayList<Doctor>> hMap = new HashMap<Integer,ArrayList<Doctor>>();
Integer hospitalId = new Intger(1);
hMap.put(hospitalId,a);
UPDATE:
For adding new doctor :
//Take the existing list from the map using hospitalId
ArrayList<Doctor> existingList = hMap.get(hospitalId);
Doctor d = new Doctor();
// add new doctor to existingList
existingList.add(d);
//put the new list again in the map
hMap.put(hospitalId,existingList);
Upvotes: 3
Reputation: 909
Many Doctor can relate to 1 hospital id.So , there is one to many mapping. I think you should use
Map(Set of Hospital_id, ArrayList of Dcotors)
Where set is a collection of hospital_id and it is unique , Arraylist is a collection of Doctors.
So , 1 hospital_id can contain list of doctors.
Upvotes: 2
Reputation: 26040
This sounds like you want a Multimap
. The Collections library doesn't have one of these by default, but you can build one fairly easily out of a Map
and a List
:
Map<Hospital, List<Doctor>> = new HashMap<Hospital, LinkedList<Doctor>>();
Upvotes: 0
Reputation: 718788
If you are restricting yourself to standard collection types than what you are describing requires a Map<HospitalId, Set<Doctor>>
. If you can use 3rd party libraries, then what you are looking for is a "multimap".
The choice between different implementation classes (HashSet
versus TreeSet
and so on) depends on the way that you intend to use the data structures.
Upvotes: 5