Manjunath G
Manjunath G

Reputation: 45

How to add Map<String, string> into Map<String, List<Object>> in java

public Map<String, List<PatientInfo>> getPatients(String sendingApplication,String sendingFacility) {
        // TODO Auto-generated method stub
        Map<String, List<PatientInfo>> patientMap = null;
        List<PatientInfo> patientList = null;
        patientMap = new HashMap<String, List<PatientInfo>>();
        patientList = new ArrayList<PatientInfo>();
        try {
        PatientInfoDAO patientInfoDAO = new PatientInfoDAOImpl();
        ItemCollection<QueryOutcome> items = patientInfoDAO.getPatients(sendingApplication, sendingFacility);

        for(Item item : items){
            PatientInfo patient = new PatientInfo();
            patient.setAdministrativeSex(item.getString(""));
            patient.setFamilyName(item.getString("FAMILYNAME"));
            patient.setGivenName(item.getString("GIVENNAME"));
            patient.setAdmitDateOrTime(item.getString("GENDER"));
            patient.setAssignedPatientLocationBuilding(item.getString("USERNAME"));
            patient.setAssignedPatientLocationFloor(item.getString("PASSWORD"));
            patient.setAssignedPatientLocationPersonLocationType(item.getString("USERROLE"));
            patient.setAssignedPatientLocationRoom(item.getString("USERSTATUS"));
            patient.setAsssignedPatientLocationBed(item.getString("EMAIL"));
            patient.setAttendingDoctor(item.getString("EMROPERATOR"));
            patient.setClientId(item.getString("clientId"));
            patient.setDateOrTimeOfMessage(item.getString("dateOrTimeOfMessage"));
            patient.setDischargeDateOrTime(item.getString("dischargeDateOrTime"));
            patient.setDob(item.getString("dob"));
            patient.setEventOccuredTime(item.getString("eventOccuredTime"));
            patient.setImageUrl(item.getString("imageUrl"));
            patient.setLastModifiedOn(item.getString("lastModifiedOn"));
            patient.setMessageControlId(item.getString("messageControlId"));
            patient.setNrPatientId(item.getString("nrPatientId"));
            patient.setPatientId(item.getString("patientId"));
            patient.setPatientStatus(item.getString("patientStatus"));
            patient.setPriorPatientLocationBed(item.getString("priorPatientLocationBed"));
            patient.setPriorPatientLocationBuilding(item.getString("priorPatientLocationBuilding"));
            patient.setPriorPatientLocationFloor(item.getString("priorPatientLocationFloor"));
            patient.setPriorPatientLocationPersonLocationType(item.getString("priorPatientLocationPersonLocationType"));
            patient.setPriorPatientLocationPointOfCare(item.getString("priorPatientLocationPointOfCare"));
            patient.setPriorPatientLocationRoom(item.getString("priorPatientLocationRoom"));
            patient.setReceivingFacility(item.getString("receivingFacility"));
            patient.setRecevingApplication(item.getString("recevingApplication"));
            patient.setSendingApplicaation(item.getString("sendingApplicaation"));
            patient.setSendingFacility(item.getString("sendingFacility"));

            patientList.add(patient);
        }

        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        patientMap.put("PATIENTLIST", patientList);
        patientMap.put("LASTKEY", date);


        return patientMap;

        }catch(AmazonServiceException ase){

            throw new RuntimeException("internalServerError");
        }catch(AmazonClientException ase){

            throw new RuntimeException("internalServerError");
        }
    }       

In this scenario i couldn't add String of date into Map>?

Upvotes: 3

Views: 2425

Answers (4)

KayV
KayV

Reputation: 13845

Here are the small changes which you can do to achieve above (Definitely you have to change the return type):

public Map<String, PatientMapObject> getPatients(String sendingApplication,String sendingFacility) {
    // TODO Auto-generated method stub
    Map<String, PatientMapObject> patientMap = null;
    List<PatientInfo> patientList = null;
    patientMap = new HashMap<String, PatientMapObject>();
    patientList = new ArrayList<PatientInfo>();
    try {

        PatientInfoDAO patientInfoDAO = new PatientInfoDAOImpl();
        ItemCollection<QueryOutcome> items = patientInfoDAO.getPatients(sendingApplication, sendingFacility);

       for(Item item : items){
            PatientInfo patient = new PatientInfo();

            patient.setAdministrativeSex("Male");

            patientList.add(patient);
        }

        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

        PatientMapObject pmo = new PatientMapObject();
        pmo.setPatientList(patientList);
        pmo.setPDate(date);

        patientMap.put("PATIENTLIST", pmo);
        return patientMap;
    }catch(Exception ase){

        throw new RuntimeException("internalServerError");
    }
}

class PatientMapObject{


    private List<PatientInfo> patientList;
    private String pdate;

    public void setPDate(String pdate) {
        this.pdate = pdate; 
    }

    public void setPatientList(List<PatientInfo> patientList) {
        this.patientList = patientList;
    }

    //getters 
}

Upvotes: 2

Robby Cornelissen
Robby Cornelissen

Reputation: 97152

As mentioned in my comment, you could use a map of type Map<String, Object> to hold both the patient list and the date. The problem with that solution is that

  1. it's not type-safe;
  2. it's vulnerable to typos in the names of map keys;
  3. you would have to perform a cast whenever you retrieve an item from the map, as in the example below.
Map<String, Object> patientData = new HashMap<>();
patientData.put("LASTKEY", date);

String lastKey = (String) patientData.get("LASTKEY");

To avoid all that, here's what I consider to be the most idiomatic Java solution: create a PatientData class to hold both your list of PatientInfo items, as well as the additional date you want to store:

public class PatientData {
    private List<PatientInfo> patientList = new ArrayList<>();
    private Date lastKey; // or better still, use one of the Java 8 date classes

    // getters and setters...
}

This guarantees type safety and eliminates both the risk of typos in the key names, and the need for casting:

PatientData patientData = new PatientData();
patientData.setLastKey(new Date());

Date date = patientData.getLastKey();

Upvotes: 0

Tadija Bagarić
Tadija Bagarić

Reputation: 2701

You are trying to place a string where a map expects a List.

Instead of:

patientMap.put("PATIENTLIST", patientList);
patientMap.put("LASTKEY", date);

Place:

patientMap.put(date, patientList);

With a map where date string is key and list of patient is value you can quickly get a list of patients for a given date.


If you want to use a map to hold a date and list of objects in string form, then you would have to convert back those strings back to their original date or list of patient objects.

If this is really what you want I suggest you look into java object serialization and deserialization.

Upvotes: 4

hiren
hiren

Reputation: 1812

You can only add the defined type of object in Map's definition (i.e List<PatientInfo> because you are creating map like Map<String, List<PatientInfo>>) to add String also you should use Map<String,Object>

Upvotes: 0

Related Questions