Authored by hugufei

优化innerJoin方法

1 package com.yoho.search.recall.scene.beans.persional; 1 package com.yoho.search.recall.scene.beans.persional;
2 2
  3 +import com.yoho.search.base.utils.CollectionUtils;
  4 +import com.yoho.search.base.utils.Transfer;
3 import com.yoho.search.core.personalized.models.SortPriceArea; 5 import com.yoho.search.core.personalized.models.SortPriceArea;
4 import com.yoho.search.core.personalized.models.UserPersionalFactorRsp; 6 import com.yoho.search.core.personalized.models.UserPersionalFactorRsp;
5 import com.yoho.search.recall.scene.models.PagePersionalFactor; 7 import com.yoho.search.recall.scene.models.PagePersionalFactor;
@@ -11,6 +13,7 @@ import org.springframework.stereotype.Component; @@ -11,6 +13,7 @@ import org.springframework.stereotype.Component;
11 13
12 import java.util.ArrayList; 14 import java.util.ArrayList;
13 import java.util.List; 15 import java.util.List;
  16 +import java.util.Map;
14 17
15 @Component 18 @Component
16 public class QueryUserPersionalFactorBean { 19 public class QueryUserPersionalFactorBean {
@@ -35,8 +38,8 @@ public class QueryUserPersionalFactorBean { @@ -35,8 +38,8 @@ public class QueryUserPersionalFactorBean {
35 //2、获取用户的个性化因子 38 //2、获取用户的个性化因子
36 UserPersionalFactorRsp userFactor = userComponent.queryUserPersionalFactor(recallParams.getUid(), recallParams.getUdid()); 39 UserPersionalFactorRsp userFactor = userComponent.queryUserPersionalFactor(recallParams.getUid(), recallParams.getUdid());
37 //3、join获取最终的结果 40 //3、join获取最终的结果
38 - List<Integer> brandIds = this.innerJoin(pageFactor.getBrandIds(),userFactor.getBrandIds(),10);  
39 - List<SortPriceArea> sortPriceAreas = this.innerJoin(pageFactor.getSortPriceAreas(),userFactor.getSortPriceAreas(),5); 41 + List<Integer> brandIds = this.innerJoin(pageFactor.getBrandIds(),userFactor.getBrandIds(),brandIdMapkeyTransfer,10);
  42 + List<SortPriceArea> sortPriceAreas = this.innerJoin(pageFactor.getSortPriceAreas(),userFactor.getSortPriceAreas(),sortPriceMapkeyTransfer,5);
40 return new UserPersionalFactorRsp(brandIds, sortPriceAreas,userFactor.getVector()); 43 return new UserPersionalFactorRsp(brandIds, sortPriceAreas,userFactor.getVector());
41 }catch (Exception e){ 44 }catch (Exception e){
42 logger.error(e.getMessage()); 45 logger.error(e.getMessage());
@@ -44,11 +47,32 @@ public class QueryUserPersionalFactorBean { @@ -44,11 +47,32 @@ public class QueryUserPersionalFactorBean {
44 } 47 }
45 } 48 }
46 49
47 - private <T> List<T> innerJoin(List<T> aList,List<T> bList,int size) { 50 + static Transfer<Integer,String> brandIdMapkeyTransfer = new Transfer<Integer, String>() {
  51 + @Override
  52 + public String transfer(Integer brandId) {
  53 + return String.valueOf(brandId);
  54 + }
  55 + };
  56 +
  57 + static Transfer<SortPriceArea,String> sortPriceMapkeyTransfer = new Transfer<SortPriceArea, String>() {
  58 + @Override
  59 + public String transfer(SortPriceArea sortPriceArea) {
  60 + StringBuilder sb = new StringBuilder();
  61 + sb.append(sortPriceArea.getMiddleSortId());
  62 + sb.append(":");
  63 + sb.append(sortPriceArea.getPriceArea());
  64 + return sb.toString();
  65 + }
  66 + };
  67 +
  68 +
  69 + private <T> List<T> innerJoin(List<T> aList, List<T> bList, Transfer<T,String> mapKeyTransfer,int size) {
  70 + Map<String,T> aKeyValueMap = CollectionUtils.toMap(aList,mapKeyTransfer);
48 List<T> results = new ArrayList<T>(); 71 List<T> results = new ArrayList<T>();
49 - for (T a : aList) {  
50 - if (bList.contains(a)) {  
51 - results.add(a); 72 + for (T b : bList) {
  73 + String bKey = mapKeyTransfer.transfer(b);
  74 + if (aKeyValueMap.containsKey(bKey)) {
  75 + results.add(aKeyValueMap.get(bKey));
52 } 76 }
53 if (results.size() >= size) { 77 if (results.size() >= size) {
54 break; 78 break;