java - 如何找到不同的流派然后添加到数组链表中

标签 java arrays linked-list hashmap hashtable

我正在尝试从电影文件中获取流派,然后检查流派是否相同。因此,如果文件中存在相同类型的电影,它们应该存储在同一个链接列表数组中。

我需要检查该类型的哈希码,然后查明文件中是否有任何其他电影具有相同类型的哈希码,然后将该特定类型放入链接列表中。所以基本上到目前为止我已经有了这个,而且我还有一个链接列表类等并且没有使用 java.util

public class LoadingMovies {

    private static final int size = 22;
    private static HashMap<String, Movies> hash = new HashMap(size);
    private static HashEntry<String, Movies> hasher = new HashEntry();
    private static List<Movies> linked = new List<>();

    public static void loadMovies(String filename) throws FileNotFoundException {
        String split = ","; //split with comma

        Scanner in = new Scanner(new File(filename));

        ArrayList<String> arraylist = new ArrayList<>();
        String wordIn;
        Movies movie = new Movies();

        while (in.hasNextLine()) {
            wordIn = in.nextLine();
            String splitter[] = wordIn.split(split);

            String movieTitle = splitter[0];
            String movieGenre = splitter[1];
            String ageRating = splitter[2];
            double scoreRating = Double.parseDouble(splitter[3]);

            movie.setTitle(movieTitle);
            movie.setGenre(movieGenre);
            movie.setAgeRating(ageRating);
            movie.setScoreRating(scoreRating);
            //System.out.println(movie.getGenre());
              arraylist.add(movie.getGenre);
//            hash.find(movie.getGenre().hashCode());
//            hash.insert(movie.getGenre().hashCode(), movie);
        }



      }
}

这就是我到目前为止所拥有的。我已经读入文件了,现在我想检查文件中的流派(字符串)是否相同,然后将该流派添加到链接列表中。我该怎么做?

最佳答案

一个HashMap<String, List<Movie>>似乎就是您要找的:

Map<String, List<Movie>> movieGenres = new HashMap<>();
while (in.hasNextLine()) {
    // code you have

    List<Movie> moviesInThisGenre = moviewGenres.get(genre);
    if (moviesInThisGenre == null) {
        moviesInThisGenre = new LinkedList<>();
        movieGenres.put(genre, moviesInThisGenre);
    }
    moviesInThisGenre.add(movie);
}

关于java - 如何找到不同的流派然后添加到数组链表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33968719/

相关文章:

c - 无法从链表中删除最低值

java - akka-spring : How to wire actors

java - 如何找到矩阵中的最短路径

c - 如何从文件中扫描字符串并将其存储在字符串数组(二维数组)中?

arrays - 求数组中除0之外的最小值

c - 使用 fgets 和链表

java - 我只想在多级继承中调用子类构造函数?

java - iText 7 : image field (button) without border nor background (transparent)

c - 在c中打印出字符串数组的元素

c - 在 C 中迭代链表时空检查行为不正确