java collection
类型(ArrayList,HashMap
等),我可以将hospital ID
和Doctor
对象存储在合适的java collection
类型中{{1}等等)。
要求是我应该能够将ArrayList,HashMap
存储为HospitalID
,将key
对象存储为Doctor
。
此外,我应该能够为各种Doctor对象提供相同的密钥(因为可能有许多医生为该医院工作)。那么我可以用于这种情况的value
集合类型(java
等)是什么?
注意:我无法使用ArrayList,HashMap
- 因为它需要唯一的ID。
稍后,我应该能够过滤掉所有为特定医院工作的医生(通过搜索它的ID),并显示其记录
答案 0 :(得分:5)
如果您将自己限制在标准集合类型之外,则需要使用Map<HospitalId, Set<Doctor>>
。如果您可以使用第三方库,那么您要寻找的是“多图”。
不同实现类(HashSet
与TreeSet
之间的选择等)取决于您打算使用数据结构的方式。
答案 1 :(得分:3)
您可以拥有ArrayList
个Doctor
个对象,然后创建一个HashMap
来存储HospitalID
作为关键字,ArrayList
个医生作为值:
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);
更新:
添加新医生:
//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);
答案 2 :(得分:2)
许多医生可以与1个医院id相关。因此,有一对多的映射。 我认为你应该使用
地图(医院数据集,Dcotors数组列表)
其中set是hospital_id的集合,并且它是唯一的,Arraylist是医生的集合。
因此,1 hospital_id可以包含医生名单。
答案 3 :(得分:1)
您需要查看区别b / w列表和地图,并根据您的要求选择。
<强>列表:强>
有序集合(也称为序列)。该接口的用户可以精确控制列表中每个元素的插入位置。用户可以通过整数索引(列表中的位置)访问元素,并搜索列表中的元素。
<强>地图强>
将键映射到值的对象。地图不能包含重复的键;每个键最多可以映射一个值。
如果您的ID也与其他医生相同,那么您可以将ID与Doctor结合使用,并将其作为单个对象保存在ArrayList中。
答案 4 :(得分:1)
创建一个名为 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;
}
}
创建一个类 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 + "]";
}
}
现在在下面描述的客户的帮助下访问所有医生名单 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);
}
}
您将看到如下所述的输出
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]
答案 5 :(得分:0)
听起来你想要Multimap
。默认情况下,Collections库没有其中一个,但您可以轻松地从Map
和List
构建一个:
Map<Hospital, List<Doctor>> = new HashMap<Hospital, LinkedList<Doctor>>();
答案 6 :(得分:0)
最好使用**地图&gt; ** **地图&gt; **。列出所有在ArrayList中设置的医生。订单权利没有任何优势。 Go with Set它将提供比Map&gt;中的ArrayList更好的性能。