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());
}
}
}
}
}
-
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– Null2020年01月08日 23:09:54 +00:00Commented 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\$Roland Illig– Roland Illig2020年01月09日 03:32:35 +00:00Commented 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\$Roland Illig– Roland Illig2020年01月09日 03:34:14 +00:00Commented 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– RoToRa2020年01月09日 11:17:06 +00:00Commented 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– mtj2020年01月09日 15:55:59 +00:00Commented Jan 9, 2020 at 15:55
1 Answer 1
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()
).