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

Answer by Naman for 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

Few open points --

One, I would suggest using foreach for DS like List to iterate through.Take a look at How does the Java 'for each' loop work?

Two, use equals to compare strings. Help - How do I compare strings in Java?

Third, use a counter variable, e.g. int counter = 0; which can be used to count the number of duplicate occurrences as

if(actors.get(z).getName().equals(movieActors.get(n).getName())) {    counter++; //currently just saving the count, you can save the name as well}

Updating from the comments discussion, to keep a count of actors that appeared in different movies :

You can maintain a Map<Actor,Count> (not literally | more of Map<String, Integer>) in general to store the count. and inside your if increment the count for each actor key.

Further to keep a unique list of actors, would suggest using Set instead of ArrayList. Please read How to use java.Set


Viewing all articles
Browse latest Browse all 7

Trending Articles



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