Authored by hugufei

优化innerJoin方法

package com.yoho.search.recall.scene.beans.persional;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.Transfer;
import com.yoho.search.core.personalized.models.SortPriceArea;
import com.yoho.search.core.personalized.models.UserPersionalFactorRsp;
import com.yoho.search.recall.scene.models.PagePersionalFactor;
... ... @@ -11,6 +13,7 @@ import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class QueryUserPersionalFactorBean {
... ... @@ -35,8 +38,8 @@ public class QueryUserPersionalFactorBean {
//2、获取用户的个性化因子
UserPersionalFactorRsp userFactor = userComponent.queryUserPersionalFactor(recallParams.getUid(), recallParams.getUdid());
//3、join获取最终的结果
List<Integer> brandIds = this.innerJoin(pageFactor.getBrandIds(),userFactor.getBrandIds(),10);
List<SortPriceArea> sortPriceAreas = this.innerJoin(pageFactor.getSortPriceAreas(),userFactor.getSortPriceAreas(),5);
List<Integer> brandIds = this.innerJoin(pageFactor.getBrandIds(),userFactor.getBrandIds(),brandIdMapkeyTransfer,10);
List<SortPriceArea> sortPriceAreas = this.innerJoin(pageFactor.getSortPriceAreas(),userFactor.getSortPriceAreas(),sortPriceMapkeyTransfer,5);
return new UserPersionalFactorRsp(brandIds, sortPriceAreas,userFactor.getVector());
}catch (Exception e){
logger.error(e.getMessage());
... ... @@ -44,11 +47,32 @@ public class QueryUserPersionalFactorBean {
}
}
private <T> List<T> innerJoin(List<T> aList,List<T> bList,int size) {
static Transfer<Integer,String> brandIdMapkeyTransfer = new Transfer<Integer, String>() {
@Override
public String transfer(Integer brandId) {
return String.valueOf(brandId);
}
};
static Transfer<SortPriceArea,String> sortPriceMapkeyTransfer = new Transfer<SortPriceArea, String>() {
@Override
public String transfer(SortPriceArea sortPriceArea) {
StringBuilder sb = new StringBuilder();
sb.append(sortPriceArea.getMiddleSortId());
sb.append(":");
sb.append(sortPriceArea.getPriceArea());
return sb.toString();
}
};
private <T> List<T> innerJoin(List<T> aList, List<T> bList, Transfer<T,String> mapKeyTransfer,int size) {
Map<String,T> aKeyValueMap = CollectionUtils.toMap(aList,mapKeyTransfer);
List<T> results = new ArrayList<T>();
for (T a : aList) {
if (bList.contains(a)) {
results.add(a);
for (T b : bList) {
String bKey = mapKeyTransfer.transfer(b);
if (aKeyValueMap.containsKey(bKey)) {
results.add(aKeyValueMap.get(bKey));
}
if (results.size() >= size) {
break;
... ...