Quantcast
Channel: How do I go through 2 list and check if the content of one element in the same as another element in a different list? - Stack Overflow
Viewing all articles
Browse latest Browse all 7

How do I go through 2 list and check if the content of one element in the same as another element in a different list?

$
0
0
private List<Actor> actors = new ArrayList<>();public void addActors(Movie x){if(actors.isEmpty()){    actors.addAll(x.getListOfActors());}else{    List<Actor> movieActors = x.getListOfActors();    ArrayList<Actor> tempActors= new ArrayList<Actor>();    for(int z = 0 ; z <actors.size(); z++)    {        for(int n = 0 ; n <movieActors.size();n++)        {            if(actors.get(z).getName().equals(movieActors.get(n).getName()))                actors.get(z).addCount(); // actor count             else            {                tempActors.add(movieActors.get(n));            }        }    }    actors.addAll(tempActors);}}

So each movie object have a list of actors. And each actor objects have a int count object which is initialized at 1. Everything I run addActors(Movie x), i want to check if there is any actors in movie x that is in List actors. if there is, i want to increment count by 1 else just add it to List actors. This is my current way of doing it, i realized its wrong but i dont know how to fix it. I run the method twice or more, repeated actors will be added to List actors because of actors.addAll(tempActors). Is there a better way to do this ?

update: So the problem isnt with the string comparing. Even if i use .equals, it isnt the result i want.sample test case:

movie: "frozen"

actors: Kristen Bell, Idina Menzel, Jonathan Groff, Josh Gad

movie: "the boss"

actors: Melissa McCarthy, Kristen Bell, Peter Dinklage, Ella Anderson

so if I use addActors(movie x) on these two movies, each actors should only appear once. In the List actors. And count for kristen bell should be at 2 now. everyone else is 1.(count is number of movies they are in)

however when i run this method with these two movies, my List actors now contains:

Kristen BellIdina MenzelJonathan GroffJosh GadMelissa McCarthyPeter DinklageElla AndersonMelissa McCarthyKristen BellPeter DinklageElla AndersonMelissa McCarthyKristen BellPeter DinklageElla AndersonMelissa McCarthyKristen BellPeter DinklageElla Anderson

(sorry for long explanation, I don't know how else to explain this )


Viewing all articles
Browse latest Browse all 7

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>