简体   繁体   中英

how to retrieve data from .dat file using hash map in java only

I have a ratings.dat file. It contains 5 fields. They are userid, movieid, rating, age, moviename. movieid is unique 1 to 3450. userid 1 to 6400. rating is given out of 5.

  1. I want to load the file into a hash map, check for the conditions which is movie is having ratings > 3
  2. Top 10 movies viewed by users
  3. I want to sort the top 20 movies by the condition that users who have viewed at least 20 movies from them I want to select the top 20 movies.

I should not use SQL. I want 100% Java code. I have done till now: reading a file in hashmap and printing the content of that file.

import java.io.*;
import java.util.*;

public class Output{

  public static void main(String[] args) throws FileNotFoundException, IOException {

    HashMap< Integer, String > hash = new HashMap< Integer, String >();
    BufferedReader rd = new BufferedReader( new FileReader ("resources/ratings.dat"));
    String line = "::";

    int i = 0;
    while ((line = rd.readLine()) != null){
        hash.put(i, line);
        i++;
    }
    for ( int j = 0 ; j < hash.size() ; j++){ 
        System.out.println(hash.get(j));
    }
  }
}
  • Create a class representing Movie which has say 5 fields.
  • Iterate over the lines of files and fill the list of Movie object.
  • Use comparator or Comparable interfaces to sort list of movie objects.
  • Then call list.sort().

  • Instead of list you can use any collection like Set or MAP.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM