0
\$\begingroup\$

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());
                }
            }
        }
    }
}
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Welcome to Code Review! Please provide additional code which forms a complete program so that reviewers can run and test your code. \$\endgroup\$
    – Null
    Commented Jan 8, 2020 at 23:09
  • \$\begingroup\$ Please edit the question title to say what your code achieves instead of what you want to be done to your code. \$\endgroup\$ Commented Jan 9, 2020 at 3:32
  • \$\begingroup\$ Please add some details about how many favorite and store merchants there are usually. And what ahould happen if there are multiple matches. \$\endgroup\$ Commented Jan 9, 2020 at 3:34
  • \$\begingroup\$ Interesting information would be also: What is a "Merchant" and what is the relationship between a "store merchant" and a "favorite merchant"? Why do they use the same class, but need to matched like that? \$\endgroup\$
    – RoToRa
    Commented Jan 9, 2020 at 11:17
  • \$\begingroup\$ Furthermore, please define "optimize". In terms of runtime? (Is there a concrete problem?) In terms of readability? In terms of less lines of code? \$\endgroup\$
    – mtj
    Commented Jan 9, 2020 at 15:55

1 Answer 1

4
\$\begingroup\$

You are implementing an inner join as a nested loop join. There are several other ways to implement joins, such as hash-based or sort-merge sort. Which one is better depends on the length of the lists.

I will sketch a hash join as that usually works most of the time.

void applyFavoriteChangesToMerchantStore(){

    List<Merchant> favoriteMerchantsList = FavoriteMerchantStore.getInstance().getFavoriteMerchantsList();
    if(favoriteMerchantsList != null && !favoriteMerchantsList.isEmpty()) {
        // build the hash table
        Map<String, Merchant> favoriteMerchantById = favoriteMerchantsList.stream().collect(Collectors.toMap(Merchant::getId, Function.identity()));
        List<Merchant> storeMerchantList = MerchantStore.getInstance().getMerchantList();
        for (Merchant storeMerchant : storeMerchantList) {
            // probe the hash table
            Merchant favoriteMerchant = favoriteMerchantById.get(storeMerchant.getId())
            if(favoriteMerchant != null){
                //merchant match found
                //set merchant favorite status

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

I assumed that TextUtils.equals is equal to String.equals. If it's more lenient (e.g., case-insensitive), you need to normalize the lookup key (e.g., String.toLowerCase()).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.