So I have an arrayList of msgs, each is classified as either static or dynamic
I create a vessel or find a vessel object (from an arrayList of vessels) and add the information to the vessel depending on msg type.
VesselS vessels = new VesselS();
msgs.forEach(msg -> {
if (msg instanceof DynamicDataReport) {
DynamicDataReport dynamicDataReport = (DynamicDataReport) msg;
Vessel vessel = new Vessel(msg.getSourceMmsi().getMMSI());
Coordinate coord = new Coordinate(dynamicDataReport.getLongitude(),dynamicDataReport.getLatitude());
int index = vessels.index(vessel);
if(index < 0) { //if vessel is not present in vessels
vessel.addCoor(coord);
vessels.addVessel(vessel);
}
else{ //if vessel is already in vessels
vessels.getVessels().get(index).addCoor(coord);
}
}
This is the first part, I thought using an index variable might be easier than searching for the index twice (in the next section of code), but it didn't seem to effect the run time.
else if (msg instanceof StaticDataReport) {
StaticDataReport staticDataReport = (StaticDataReport) msg;
Vessel vessel = new Vessel(msg.getSourceMmsi().getMMSI());
String shipName = staticDataReport.getShipName();
String callSign = staticDataReport.getCallsign();
if(!vessels.contains(vessel)) { //if vessel is not present in vessels
vessel.addname(shipName);
vessel.setCallSign(callSign);
vessels.addVessel(vessel);
}
else{ //if vessel is already in vessels
vessels.getVessels().get(vessels.index(vessel)).addname(shipName);
}
}
});
First time working with a lot of information and I need to minimize runtime. Would it be worth looking into storing vessels in trees opposed to lists ? is there redundancy in my code that I can cut down on ?
My contains and index methods are just loops.
public boolean contains(Vessel vessel){
for(int i= 0; i<Vessels.size(); i++){
if(Vessels.get(i).getMMSI() == vessel.getMMSI()){
return true;
}
}
return false;
}