ADD file via upload

main
pjhmizn49 1 year ago
parent c6e786e0d8
commit 0ee075ee30

@ -0,0 +1,46 @@
package com.example.flower.unit;
import cn.hutool.core.collection.CollectionUtil;
import com.example.flower.entity.RelateDTO;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class UserCF {
/**
* : id
*
* @param userId
* @param list
* @return {@link List<Integer>}
*/
public static List<Integer> recommend(Integer userId, List<RelateDTO> list) {
// 按用户分组
Map<Integer, List<RelateDTO>> userMap = list.stream().collect(Collectors.groupingBy(RelateDTO::getUseId));
// 获取其他用户与当前用户的关系值
Map<Integer, Double> userDisMap = CoreMath.computeNeighbor(userId, userMap, 0);
if (CollectionUtil.isEmpty(userDisMap)) {
return Collections.emptyList();
}
// 获取关系最近的用户
double maxValue = Collections.max(userDisMap.values());
Set<Integer> userIds = userDisMap.entrySet().stream().filter(e -> e.getValue() == maxValue).map(Map.Entry::getKey).collect(Collectors.toSet());
// 取关系最近的用户
Integer nearestUserId = userIds.stream().findAny().orElse(null);
if (nearestUserId == null) {
return Collections.emptyList();
}
// 最近邻用户看过商品列表
List<Integer> neighborItems = userMap.get(nearestUserId).stream().map(RelateDTO::getGoodsId).collect(Collectors.toList());
// 指定用户看过商品列表
List<Integer> userItems = userMap.get(userId).stream().map(RelateDTO::getGoodsId).collect(Collectors.toList());
// 找到最近邻看过,但是该用户没看过的商品
neighborItems.removeAll(userItems);
return neighborItems;
}
}
Loading…
Cancel
Save