Skip to main content
fixed/added code formatting
Source Link
greybeard
  • 6.5k
  • 3
  • 20
  • 52

I am trying to optimize this code. The only optimization I can think of is to ?¿? a return or break statement after applyOfferChanges(...) inside the second if condition which does not work either because there could be multiple matches. I cannot either use a map for favoriteMerchantsList because getFavoriteMerchantsList() return type is a list; a list of fav.(of favorited merchants). Any ideas?

void applyFavoriteChangesToMerchantStore(){

    List<Merchant> favoriteMerchantsList = FavoriteMerchantStore.getInstance().getFavoriteMerchantsList();
    if(favoriteMerchantsList != null && !favoriteMerchantsList.isEmpty()) {
        List<Merchant> storeMerchantList = MerchantStore.getInstance().getMerchantList();
        for (Merchant storeMerchant : storeMerchantList) {
            for (Merchant favoriteMerchant: favoriteMerchantsList){
                if(TextUtils.equals(storeMerchant.getId(), favoriteMerchant.getId())){
                    //merchant match found
                    //set merchant favorite status

                   storeMerchant.setFavoriteMerchant(favoriteMerchant.getFavoriteMerchant());
                    //set offer favorite status
                    applyOfferChanges(favoriteMerchant.getOffferList(),
                            storeMerchant.getOffferList());
                }
            }
        }
    }
}

I am trying to optimize this code. The only optimization I can think of is to a return or break statement after applyOfferChanges(...) inside the second if condition which does not work either because there could be multiple matches. I cannot either use a map for favoriteMerchantsList because getFavoriteMerchantsList() return type is a list; a list of fav. merchants. Any ideas?

void applyFavoriteChangesToMerchantStore(){

    List<Merchant> favoriteMerchantsList = FavoriteMerchantStore.getInstance().getFavoriteMerchantsList();
    if(favoriteMerchantsList != null && !favoriteMerchantsList.isEmpty()) {
        List<Merchant> storeMerchantList = MerchantStore.getInstance().getMerchantList();
        for (Merchant storeMerchant : storeMerchantList) {
            for (Merchant favoriteMerchant: favoriteMerchantsList){
                if(TextUtils.equals(storeMerchant.getId(), favoriteMerchant.getId())){
                    //merchant match found
                    //set merchant favorite status

                   storeMerchant.setFavoriteMerchant(favoriteMerchant.getFavoriteMerchant());
                    //set offer favorite status
                    applyOfferChanges(favoriteMerchant.getOffferList(),
                            storeMerchant.getOffferList());
                }
            }
        }
    }
}

I am trying to optimize this code. The only optimization I can think of is to ?¿? a return or break statement after applyOfferChanges() inside the second if condition which does not work either because there could be multiple matches. I cannot either use a map for favoriteMerchantsList because getFavoriteMerchantsList() return type is a list (of favorited merchants). Any ideas?

void applyFavoriteChangesToMerchantStore(){

    List<Merchant> favoriteMerchantsList = FavoriteMerchantStore.getInstance().getFavoriteMerchantsList();
    if(favoriteMerchantsList != null && !favoriteMerchantsList.isEmpty()) {
        List<Merchant> storeMerchantList = MerchantStore.getInstance().getMerchantList();
        for (Merchant storeMerchant : storeMerchantList) {
            for (Merchant favoriteMerchant: favoriteMerchantsList){
                if(TextUtils.equals(storeMerchant.getId(), favoriteMerchant.getId())){
                    //merchant match found
                    //set merchant favorite status

                   storeMerchant.setFavoriteMerchant(favoriteMerchant.getFavoriteMerchant());
                    //set offer favorite status
                    applyOfferChanges(favoriteMerchant.getOffferList(),
                            storeMerchant.getOffferList());
                }
            }
        }
    }
}

I am trying to optimize this code. The only optimization I can think of is to a returnreturn or breakbreak statement after applyOfferChanges(...)applyOfferChanges(...) inside the second ifif condition which does not work either because there could be multiple matches. I cannot either use a map for "favoriteMerchantsList"favoriteMerchantsList because "getFavoriteMerchantsList()"getFavoriteMerchantsList() return type is a list; a list of fav. merchantmerchants. Any ideas?

void applyFavoriteChangesToMerchantStore(){

void applyFavoriteChangesToMerchantStore(){

    List<Merchant> favoriteMerchantsList = FavoriteMerchantStore.getInstance().getFavoriteMerchantsList();
    if(favoriteMerchantsList != null && !favoriteMerchantsList.isEmpty()) {
        List<Merchant> storeMerchantList = MerchantStore.getInstance().getMerchantList();
        for (Merchant storeMerchant : storeMerchantList) {
            for (Merchant favoriteMerchant: favoriteMerchantsList){
                if(TextUtils.equals(storeMerchant.getId(), favoriteMerchant.getId())){
                    //merchant match found
                    //set merchant favorite status

                   storeMerchant.setFavoriteMerchant(favoriteMerchant.getFavoriteMerchant());
                    //set offer favorite status
                    applyOfferChanges(favoriteMerchant.getOffferList(),
                            storeMerchant.getOffferList());
                }
            }
        }
    }
}

}

I am trying to optimize this code. The only optimization I can think of is to a return or break statement after applyOfferChanges(...) inside the second if condition which does not work either because there could be multiple matches. I cannot either use a map for "favoriteMerchantsList" because "getFavoriteMerchantsList()" return type is a list; a list of fav. merchant. Any ideas?

void applyFavoriteChangesToMerchantStore(){

List<Merchant> favoriteMerchantsList = FavoriteMerchantStore.getInstance().getFavoriteMerchantsList();
if(favoriteMerchantsList != null && !favoriteMerchantsList.isEmpty()) {
    List<Merchant> storeMerchantList = MerchantStore.getInstance().getMerchantList();
    for (Merchant storeMerchant : storeMerchantList) {
        for (Merchant favoriteMerchant: favoriteMerchantsList){
            if(TextUtils.equals(storeMerchant.getId(), favoriteMerchant.getId())){
                //merchant match found
                //set merchant favorite status

               storeMerchant.setFavoriteMerchant(favoriteMerchant.getFavoriteMerchant());
                //set offer favorite status
                applyOfferChanges(favoriteMerchant.getOffferList(),
                        storeMerchant.getOffferList());
            }
        }
    }
}

}

I am trying to optimize this code. The only optimization I can think of is to a return or break statement after applyOfferChanges(...) inside the second if condition which does not work either because there could be multiple matches. I cannot either use a map for favoriteMerchantsList because getFavoriteMerchantsList() return type is a list; a list of fav. merchants. Any ideas?

void applyFavoriteChangesToMerchantStore(){

    List<Merchant> favoriteMerchantsList = FavoriteMerchantStore.getInstance().getFavoriteMerchantsList();
    if(favoriteMerchantsList != null && !favoriteMerchantsList.isEmpty()) {
        List<Merchant> storeMerchantList = MerchantStore.getInstance().getMerchantList();
        for (Merchant storeMerchant : storeMerchantList) {
            for (Merchant favoriteMerchant: favoriteMerchantsList){
                if(TextUtils.equals(storeMerchant.getId(), favoriteMerchant.getId())){
                    //merchant match found
                    //set merchant favorite status

                   storeMerchant.setFavoriteMerchant(favoriteMerchant.getFavoriteMerchant());
                    //set offer favorite status
                    applyOfferChanges(favoriteMerchant.getOffferList(),
                            storeMerchant.getOffferList());
                }
            }
        }
    }
}
Source Link
leger
  • 17
  • 1

optimize nested for loops within nested if statements

I am trying to optimize this code. The only optimization I can think of is to a return or break statement after applyOfferChanges(...) inside the second if condition which does not work either because there could be multiple matches. I cannot either use a map for "favoriteMerchantsList" because "getFavoriteMerchantsList()" return type is a list; a list of fav. merchant. Any ideas?

void applyFavoriteChangesToMerchantStore(){

List<Merchant> favoriteMerchantsList = FavoriteMerchantStore.getInstance().getFavoriteMerchantsList();
if(favoriteMerchantsList != null && !favoriteMerchantsList.isEmpty()) {
    List<Merchant> storeMerchantList = MerchantStore.getInstance().getMerchantList();
    for (Merchant storeMerchant : storeMerchantList) {
        for (Merchant favoriteMerchant: favoriteMerchantsList){
            if(TextUtils.equals(storeMerchant.getId(), favoriteMerchant.getId())){
                //merchant match found
                //set merchant favorite status

               storeMerchant.setFavoriteMerchant(favoriteMerchant.getFavoriteMerchant());
                //set offer favorite status
                applyOfferChanges(favoriteMerchant.getOffferList(),
                        storeMerchant.getOffferList());
            }
        }
    }
}

}