简体   繁体   English

更新包含彼此的 ArrayLists?

[英]Updating ArrayLists that contains each other?

Apologies if there have been similar questions, I'm honestly not sure how to call this concept to search for questions.抱歉,如果有类似的问题,我真的不知道如何调用这个概念来搜索问题。

So I need to create a database with 3 classes like below:所以我需要创建一个包含 3 个类的数据库,如下所示:

public class Actor {
    private String name;
    private ArrayList<Movie> movies; //all movies the actor has been in
}

public class Movie {
    private String name;
    private ArrayList<Actor> actors;
}

public class MovieDatabase {
    private ArrayList<Movie> movieList;
    private ArrayList<Actor> actorList; //all actors in the movie
}

I have to create a method to add a movie and an actor to the database.我必须创建一个将电影和演员添加到数据库的方法。 The final goals is that the new movie needs to be of the Movie class, and contains all the actors that are in it, same for the new actor.最终目标是新电影必须属于电影 class,并且包含其中的所有演员,对于新演员也是如此。

What I cannot figure out is that, since the Movie class contains an array of Actor objects, and the Actor class contains an array of Movie list, how do you update so that in the end, the new Movie added contains a complete list of Actors in it, with each Actor in the list having their movie lists updated with the new Movie object?我无法弄清楚的是,由于电影 class 包含一个演员对象数组,而演员 class 包含一个电影列表数组,你如何更新以便最后添加的新电影包含一个完整的演员列表在其中,列表中的每个演员的电影列表都更新为新电影 object?

Is recursion the right concept to apply in this case?在这种情况下,递归是正确的概念吗?

Suppose a new Movie gets added假设添加了一部新电影

Movie movie = new Movies();

movie.setActors(listOfActors)

Now for each actor you need to update the movie list现在对于每个演员,您需要更新电影列表

listOfActors.forEach(actor -> addMovieToActor(movie,actor));


public addMovieToActor(Movie movie,Actor actor){

   List<Movies> existingMovies =actor.getMovies();

   existingMovies.add(movie);
}

Depending on your needs, you may need to take care of synchronization between updates.根据您的需要,您可能需要注意更新之间的同步。

I don't think recursion is appropriate here, although you could use it.我认为递归在这里不合适,尽管您可以使用它。 The databases have a circular dependency so you just need to synchronize after updates.数据库具有循环依赖关系,因此您只需要在更新后进行同步。 In other words, make sure that after you add a new entry, both databases are updated with the missing information.换句话说,请确保在添加新条目后,两个数据库都使用缺失的信息进行更新。 Synchronization can be made easier by swapping ArrayList out for a HashMap , you may need to refactor your classes to do this.通过将ArrayList HashMap使同步更容易,您可能需要重构您的类来执行此操作。 I left out the lists for simplicity, you can add them in as a parameter:为简单起见,我省略了列表,您可以将它们作为参数添加:

void update(String movieName, String actorName) {
    if (movies.get(movieName) == null) {
        movies.put(movieName);
    } 
    if (actors.get(actorName) == null) {
        actors.put(actorName);
    }
}

Use as:用于:

HashMap<String> actors = new HashMap<>();
HashMap<String> movies = new HashMap<>();
actors.put("foo");
movies.put("bar");
// Update happened, synchronize with what you just added
update("foo", "bar");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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