lundi 1 juin 2015

IF condition needs to be checked for all objects in a loop [on hold]

I have run into this scenario and can't figure out what needs to be done. I have an ArrayList workson that contains Workon objets. I also have a HashMap worksonmap where key is workon object (all workson arraylist objects are added to map as keys) and value is hours.

Now I have a method that accepts Workon object and integer. The scenario is that if the object is already present in ArrayList, I just need to increment the hours in the map and break the loop.if the object is not present in list, I need to add the object to the arraylist and add the object and hours to the worksonmap

It's like if condition needs to be executed for all objects of arraylist (till a match is found) before else condition is triggered only once

public void addempproj(Workon w1, int hours){
for(int i=0;i<workson.size();i++){
    if(workson.get(i).equals(w1)){
            worksonmap.put(w1, hours+worksonmap.get(w1));
            break;
        }

Aucun commentaire:

Enregistrer un commentaire