问题描述:搜索栏查寻商品时,搜索源可能是多方的,这里上搜索来源为淘宝京东唯品会的api查询接口和自定义的开放搜索源,目的:对目标位置进行开放搜索的商品穿插进api查询结果集中。
参数:api商品查询结果集,List<GoodsApi> first;
参数:api商品查询结果集,List<OpenSerach> second;
参数:api商品查询结果集,List<Integer> index;//目标位置.
例如:
first【1,3,4,6,7,8】
second【2,5】
index【1,4】
结果:
result【1,2,3,4,5,6,7,8】
问题分析:问题抽象出来就是,数组的穿插。
分析可能出现情况:
first数组为空;second数组为空;index数组为空;first数据多,second数据少;first少,second多;index内可能无序,可能重复。
@Override
public LinkedList<GoodsBaseInfoResult> crossTwoList(GoodsSearchQuery query, LinkedList<GoodsBaseInfoResult> first, LinkedList<GoodsBaseInfoResult> second, BucketApiInvokingEnum apiInvoking) {
deduplicationList(first, second, apiInvoking);
List<Integer> apiInvokingList = query.getSceneBucket().getSearchResult().get("5").getApiInvokingList();
LinkedList<GoodsBaseInfoResult> resultsList = Lists.newLinkedList();
int resultsIndex = 0;
int firstIndex = 0;
int secondIndex = 0;
int apiInvokingIndex = 0;
apiInvokingList = apiInvokingList.stream().distinct().collect(Collectors.toList());
apiInvokingList.sort((x, y) -> x - y);
while (firstIndex < first.size()) {
if (secondIndex < second.size() && apiInvokingIndex < apiInvokingList.size() && resultsIndex == apiInvokingList.get(apiInvokingIndex)) {
resultsList.add(second.get(secondIndex));
apiInvokingIndex++;
secondIndex++;
resultsIndex++;
} else {
resultsList.add(first.get(firstIndex));
firstIndex++;
resultsIndex++;
}
}
while (apiInvokingIndex < apiInvokingList.size() && secondIndex < second.size()) {
resultsList.add(second.get(secondIndex));
secondIndex++;
apiInvokingIndex++;
}
return resultsList;
}