热门电影评分案例分心

package com.doit.movie;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.alibaba.fastjson.JSON;
/**
 * 每个  用户   评分最高的N条记录  
       每个用户评分的平均值   
       最大方(评分平均值高)的N个用户
       最热门的N部电影  
      评价最高的N部电影
 * @author hang
 *
 */
public class LoadData {
    @SuppressWarnings("resource")
    public static void main(String[] args) throws Exception {

    }

    public static void question5() {
        /*最热门的N部电影  
        评价最高的N部电影*/
        Map map = new HashMap<>();
        Map> movieMap = getMovieMap();
        Set>> entrySet = movieMap.entrySet();
        for (Entry> entry : entrySet) {
            String mid = entry.getKey();
            List list = entry.getValue();
            double sum = 0.0 ;
            for (Movie movie : list) {
                sum += Double.parseDouble(movie.getRate());
            }
            double avg = sum/list.size();
            map.put(mid, avg) ;
            //对map的value排序
        }
    }

    public static void question4() {
        Map map = new HashMap<>();
        Map> movieMap = getMovieMap();
        Set>> entrySet = movieMap.entrySet();
        for (Entry> entry : entrySet) {
            String mid = entry.getKey();
            List list = entry.getValue();
            map.put(mid, list.size());
        }
        //对map按照value排序
    }

    public static void question3() {
        Map> uidMap = getUidMap();
        /*每个  用户   评分最高的3条记录  
        每个用户评分的平均值 
        最大方(评分平均值高)的N个用户*/
        Map map = new HashMap<>() ;
        Set>> entrySet = uidMap.entrySet();
        for (Entry> entry : entrySet) {
            String uid = entry.getKey();
            List list = entry.getValue();
            double sum = 0 ;
            for (Movie movie : list) {
                sum+=Double.parseDouble( movie.getRate());
            }
            double avg = sum/list.size() ;
        //  System.out.println(uid+"--->"+avg);
            map.put(uid, avg);
            //map的value排序
        }
    }
    public static void question2() {
        Map> uidMap = getUidMap();
        /*每个  用户   评分最高的3条记录  
        每个用户评分的平均值 
        最大方(评分平均值高)的N个用户*/
        Set>> entrySet = uidMap.entrySet();
        for (Entry> entry : entrySet) {
            String uid = entry.getKey();
            List list = entry.getValue();
            double sum = 0 ;
            for (Movie movie : list) {
                sum+=Double.parseDouble( movie.getRate());
            }
            double avg = sum/list.size() ;
            System.out.println(uid+"--->"+avg);
        }
    }

    public static void question1() {
        Map> uidMap = getUidMap();
        /*每个  用户   评分最高的3条记录 
        每个用户评分的平均值 
        最大方(评分平均值高)的N个用户*/
        Set>> entrySet = uidMap.entrySet();

        for (Entry> entry : entrySet) {
            String uid = entry.getKey();
            List list = entry.getValue();
            if(list!=null && list.size()>=3){
                //按照分数排序
                Collections.sort(list,new Comparator() {
                    @Override
                    public int compare(Movie o1, Movie o2) {
                        // TODO Auto-generated method stub
                        return o2.getRate().compareTo(o1.getRate());
                    }
                });
                for (int i = 0; i < 3; i++) {
                    Movie m = list.get(i);
                    System.out.println(uid+"---->"+m);
                }
            }
        }
    }

    /**
     * 获取以用户id为key的map
     */
    public static Map> getUidMap() {
        Map> map = new HashMap<>();
        // 读取文件数据
        try (BufferedReader br = new BufferedReader(new FileReader("d:/data/movie.txt"));) {
            String line = null;
            while ((line = br.readLine()) != null) {
                Movie m;
                try {
                    m = JSON.parseObject(line, Movie.class);
                } catch (Exception e) {
                    continue;
                }
                List list = map.getOrDefault(m.getUid(), new ArrayList<>());
                list.add(m);
                map.put(m.getUid(), list);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 获取以电影的id为key的map
     */
    public static Map> getMovieMap() {
        Map> map = new HashMap<>();
        // 读取文件数据
        try (BufferedReader br = new BufferedReader(new FileReader("d:/data/movie.txt"));) {
            String line = null;
            while ((line = br.readLine()) != null) {
                Movie m;
                try {
                    m = JSON.parseObject(line, Movie.class);
                } catch (Exception e) {
                    continue;
                }
                List list = map.getOrDefault(m.getMovie(), new ArrayList<>());
                list.add(m);
                map.put(m.getMovie(), list);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }
}

你可能感兴趣的:(热门电影评分案例分心)