1

1つの病院に複数の医師がいます。そして、私は私が保存できるタイプjava collection(など)と適切なタイプ(など)のオブジェクトを知る必要があります。ArrayList,HashMaphospital IDDoctorjava collectionArrayList,HashMap

HospitalID要件は、をとしてkeyDoctorオブジェクトをそのまま保存できる必要があることvalueです。

さらに、さまざまなDoctorオブジェクトに対して同じキーを使用できる必要があります(この病院では多くの医師が働いている可能性があるため)。では、このシナリオで使用できるjavaコレクションタイプ(など)は何ですか?ArrayList,HashMap

注:HashMap一意のIDを使用するため、-は使用できません。

後で、特定の病院で働くすべての医師を(IDから検索して)除外し、その記録を表示できるようになるはずです。

4

7 に答える 7

5

説明しているものよりも標準のコレクションタイプに制限している場合は、が必要Map<HospitalId, Set<Doctor>>です。サードパーティのライブラリを使用できる場合、探しているのは「マルチマップ」です。

異なる実装クラス(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に関連付けることができます。したがって、1対多のマッピングがあります。私はあなたが使うべきだと思います

Map(Hospital_idのセット、DcotorsのArrayList)

setがhospital_idのコレクションであり、一意である場合、ArraylistはDoctorsのコレクションです。

したがって、1つのhospital_idに医師のリストを含めることができます。

于 2012-12-19T04:52:50.297 に答える
1

リストとマップの違いを確認し、要件に応じて選択する必要があります。

リスト:

順序付けられたコレクション(シーケンスとも呼ばれます)。このインターフェースのユーザーは、リストのどこに各要素を挿入するかを正確に制御できます。ユーザーは、整数インデックス(リスト内の位置)で要素にアクセスし、リスト内の要素を検索できます。

地図:

キーを値にマップするオブジェクト。マップに重複するキーを含めることはできません。各キーは最大で1つの値にマップできます。

IDが他の医師と同じである場合は、IDをDoctorと組み合わせて、単一のオブジェクトとしてArrayListに保存できます。

于 2012-12-19T04:48:10.943 に答える
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 に答える
0

これはあなたが欲しいように聞こえますMultimap。コレクションライブラリにはデフォルトでこれらのいずれかがありませんが、aMapList:からかなり簡単に構築できます。

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

**マップ>****マップ>**を使用することをお勧めします。ArrayListのSetinstedにすべての医師をリストします。注文を正しく守るための優先順位はありません。Setを使用すると、Map>のArrayListよりもパフォーマンスが向上します。

于 2013-12-11T06:45:04.367 に答える