1

1 家医院将有多名医生。我需要知道我可以存储的java collection类型(等)和合适类型(等)的对象。ArrayList,HashMaphospital IDDoctorjava collectionArrayList,HashMap

要求是我应该能够将 存储HospitalID为 akey并将Doctor对象存储为value

此外,我应该能够为各种 Doctor 对象拥有相同的密钥(因为可以有很多医生为所述医院工作)。那么我可以在这种情况下使用的java集合类型(等)是什么?ArrayList,HashMap

注意:我不能使用HashMap- 因为它需要唯一的 ID。

稍后我应该能够过滤掉为特定医院工作的所有医生(通过从其 ID 中搜索),并显示其记录

4

7 回答 7

5

如果您将自己限制为标准集合类型,而不是您所描述的需要Map<HospitalId, Set<Doctor>>. 如果您可以使用 3rd 方库,那么您正在寻找的是“多图”。

不同实现类(HashSetTreeSet等)之间的选择取决于您打算使用数据结构的方式。

于 2012-12-19T04:49:18.787 回答
3

您可以拥有一个对象,然后创建一个存储作为键和医生作为值ArrayList的对象:DoctorHashMapHospitalIDArrayList

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);
于 2012-12-19T04:50:59.443 回答
2

许多医生可以与 1 个医院 ID 相关联。因此,存在一对多映射。我认为你应该使用

地图(Hospital_id 集,医生的 ArrayList)

其中 set 是 hospital_id 的集合,它是唯一的,Arraylist 是 Doctors 的集合。

因此,1 个 hospital_id 可以包含医生列表。

于 2012-12-19T04:52:50.297 回答
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]
于 2012-12-24T07:08:56.980 回答
1

您需要查看黑白列表和地图的差异,并根据您的要求进行选择。

列表:

有序集合(也称为序列)。此界面的用户可以精确控制每个元素在列表中的插入位置。用户可以通过整数索引(列表中的位置)访问元素,并在列表中搜索元素。

地图:

将键映射到值的对象。地图不能包含重复的键;每个键最多可以映射到一个值。

如果您的 ID 也与其他医生相同,那么您可以将 ID 与 Doctor 结合起来,并将其作为单个对象保存在 ArrayList 中。

于 2012-12-19T04:48:10.943 回答
0

最好使用** Map> ** instered of ** Map>**。列出 ArrayList 的 Set insted 中的所有医生。没有优先权遵循正确的顺序。使用 Set 会比 Map> 中的 ArrayList 提供更好的性能

于 2013-12-11T06:45:04.367 回答
0

这听起来像你想要一个Multimap. Collections 库默认情况下没有其中之一,但您可以很容易地从 aMap和 a构建一个List

Map<Hospital, List<Doctor>> = new HashMap<Hospital, LinkedList<Doctor>>();
于 2012-12-19T04:50:10.080 回答