1.从数据库获取到的数据(没有办法一次性取回数据)
//搜索商店
List searchShopVO = shopInfoMapper.searchShop(searchContent, type,locationId,orderType,pageNums,lastTime);
//搜索服务
List searchServiceVO = shopInfoMapper.searchService(searchContent, type, locationId, orderType, pageNums, lastTime);
//SearchShopVO类如下
public class SearchShopVO {
private Integer shopId;
private String shopLogoUrl;
private String shopName;
private List shopServiceType;
private String totalScore;
private String address;
private String longitude;
private String latitude;
private String updTime;
private List shopService;
//get set方法省略
……
}
工程需求: 1.合并searchShopVO与searchServiceVO,组成searchShopVO 2.searchShopVO去重,条件是shopId 3.返回searchShopVO即可 数据库请求回来的数据 searchShopVO={ 1000000003 1000000009 1000000008 1000000007 1000000006 1000000005 1000000004 1000000002 1000000001 1000000000 } searchServiceVO={ 1000000003 1000000004 1000000002 1000000001 1000000000
} 很明显,重复了5条记录(03,04,02,01,00) 下面是去重方法
//1.合并到一个list中
searchShopVO.addAll(searchServiceVO);
//2.取出所有的shopId
List resultListInteger = new ArrayList();
List resultListSearchShopVO = new ArrayList();
for (int i = 0; i < searchShopVO.size(); i++) {
resultListInteger.add(searchShopVO.get(i).getShopId());
}
//3.shopId去重
List tempList= new ArrayList();
for (int i = 0; i < resultListInteger.size(); i++) {
if (!tempList.contains(resultListInteger.get(i))) {
tempList.add(resultListInteger.get(i));
resultListSearchShopVO.add(searchShopVO.get(i));
}
}
最后返回的结果是resultListSearchShopVO