代码优化
This commit is contained in:
@@ -1,11 +1,9 @@
|
|||||||
package com.tarzan.recommend.core;
|
package com.tarzan.recommend.core;
|
||||||
|
|
||||||
import com.tarzan.recommend.dto.RelateDTO;
|
import com.tarzan.recommend.dto.RelateDTO;
|
||||||
import org.assertj.core.util.Lists;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.IntStream;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 核心算法
|
* 核心算法
|
||||||
|
|||||||
@@ -32,7 +32,10 @@ public class UserCF {
|
|||||||
double maxValue=Collections.max(userDisMap.values());
|
double maxValue=Collections.max(userDisMap.values());
|
||||||
Set<Integer> userIds=userDisMap.entrySet().stream().filter(e->e.getValue()==maxValue).map(Map.Entry::getKey).collect(Collectors.toSet());
|
Set<Integer> userIds=userDisMap.entrySet().stream().filter(e->e.getValue()==maxValue).map(Map.Entry::getKey).collect(Collectors.toSet());
|
||||||
//取关系最近的用户
|
//取关系最近的用户
|
||||||
Integer nearestUserId = userIds.stream().findAny().get();
|
Integer nearestUserId = userIds.stream().findAny().orElse(null);
|
||||||
|
if(nearestUserId==null){
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
//最近邻用户看过电影列表
|
//最近邻用户看过电影列表
|
||||||
List<Integer> neighborItems = userMap.get(nearestUserId).stream().map(RelateDTO::getItemId).collect(Collectors.toList());
|
List<Integer> neighborItems = userMap.get(nearestUserId).stream().map(RelateDTO::getItemId).collect(Collectors.toList());
|
||||||
//指定用户看过电影列表
|
//指定用户看过电影列表
|
||||||
|
|||||||
Reference in New Issue
Block a user