Alicia Sykes
Alicia Sykes

Reputation: 7117

Java: How to merge objects sort of

so I've got a List of DAO objects that have come strait from an SQL database.

They have the following fields petId, petWeight, petAge, drugCode, drugStrength, dayOfTreatment.

And I need to put it into another object that will have one object for each pet (the only difference will be multiple days in one object, will be a list)

petId | petWeight | petAge | drugCode | drugStrength | dayOfTreatment
 01     5kg         2years   XCJJL      20mg           0
 01     5kg         2years   XCJJL      20mg           1
 01     5kg         2years   XCJJL      20mg           2
 01     5kg         2years   XCJJL      20mg           3
 02     9kg         6years   XUKKG      80mg           0
 02     9kg         6years   XUKKG      80mg           1
 02     9kg         6years   XUKKG      80mg           2
 02     9kg         6years   XUKKG      80mg           3
 02     9kg         6years   XUKKG      80mg           4
 03     7kg         4years   XDDDD      120mg          0

Convert to:

petId | petWeight | petAge | drugCode | drugStrength | daysOfTreatment
01      5kg         2years   XCJJL      20mg           0,1,2,3
02      9kg         6years   XUKKG      80mg           0,1,2,3,4
03      7kg         4years   XDDDD      120mg          0

In the data above is is simplified a little bit, there are a few more fields, and the days of treatment may not always be consecutive days, e.g. day 0,3,6,9 or day 0,7,14... And this is a small part of a much bigger program, that's why I have to make the objects in a set format.

It's mainly the syntax I'm stuck on, I just can't find a way to do this, I have a loop at the moment going through each of the objects

Edit: I've got to do this in Java

I'd be very grateful for any suggestions or code snippets, thanks in advance

Upvotes: 0

Views: 197

Answers (3)

bsd
bsd

Reputation: 2727

While iterating through the resultant DAOs. Keep a Map whose key is petId(I presume this is unique) and the value is the Pet object.

So the class Pet might look like

    class Pet {
        int petId;
        int petWeightKg; //in kg
        Map<String, PetDosage> dosage; //drugCode to PetDosage

        //other getters and setters  

    } 

    class PetDosage {

        int drugStrengthMg ;//mg
        List<Integer> daysOfTreatment;

        //other getters and setters          
    } 

So here is the pseudo code

    Map<Integer, Pet> petIdToPet = ...
    for (PetDAO petDAO : listPetDAOs) {
        int id = petDAO.getId();
        Pet pet = petIdToPet.get(id);
        if (pet == null) { //woo new pet
            petIdToPet.put(id, new Pet( .... ));
        } else {
            //Depending upon the other data
           // update fields like dosage, days
           //increment drugStrength, 
           //Add List of Days etc 

        }  
    }

Upvotes: 0

Alankar Srivastava
Alankar Srivastava

Reputation: 864

A crude way of doing this can be like this.

    HashMap<String, petObject> petObjMap = new HashMap<String, petObject>();
    for(PetObject petObject : petObjectList){

        String id = petObject.getId();
        PetObject groupedObj = petObjMap.get(id);
        if( groupedObj == null ){
            petObjMap.put(id, petObject);
        }else{
            groupedObj.setDayOfTreatment(groupedObj.getDayOfTreatment + "," + petObject.getDayOfTreatment());
        }
    }

But as you mentioned you are already using for loop, so might be this is the way you are doing it !!

Upvotes: 3

mthmulders
mthmulders

Reputation: 9705

In plain SQL, you would use group by petId, petWeight, petAge, drugCode, drugStrength. The main challenge would be how to generate the daysOfTreatment column. As far as I know, there is no standard SQL construct to do that, but some database vendors have specific functions that allow you to do that.

For example, in MySQL, you could use GROUP_CONCAT like this:

select petId,
       petWeight,
       petAge,
       drugCode,
       drugStrength,
       group_concat(dayOfTreatment separator ',')
from   <table name>
group by
       petId,
       petWeight,
       petAge,
       drugCode,
       drugStrength

Upvotes: 1

Related Questions