Fly社区基于Redis推送帖子的核心算法

2022-10-27 16:36:06 浏览数 (2)

「Talk is cheap. Show me the code」

代码语言:javascript复制
 String key = user.getUserId()   user.getEmail();
        List<String> likes = likeMapper.queryLikenames();
        Map<String,Integer> map = new HashMap();

        for (String like : likes) {
            if (like != null && cluster.hget(key,like) != null) {
                Integer likeDegree = Integer.parseInt(cluster.hget(key,like));
                map.put(like,likeDegree);
            }
        }

        // 遍历map找出前三爱好
        String[] like= new String[];
        int[] value = new int[];
        for (String likeKey : map.keySet()) {
            int likeValue = map.get(likeKey);
            for (int i = ; i < value.length; i  ) {
                if (likeValue > value[i]) {
                    // 替换
                    for (int j = value.length-; j > i; j--) {
                        value[j] = value[j-];
                        like[j] = like[j-];
                    }
                    value[i] = likeValue;
                    like[i]= likeKey;
                    break;
                }
            }
        }
        String bestLike = like[];
        String middleLike = like[];
        String lastLike = like[];

        int total = value[]   value[]   value[];
        if (total == ) {
            total = ;
        }

0 人点赞