2

Let's say I have two Arraylists.

    a.add("Isabella");
    a.add("Angelina");
    a.add("Pille");
    a.add("Hazem");

    b.add("Isabella");
    b.add("Angelina");
    b.add("Bianca");

    a.retainAll(b);

This should give me Arraylist a with the following elements: Isabella, Angelina, Pille, Hazem. But, when I try a.size() I get 0. Why?

My output:

[DEBUG] The Karate Kid
[DEBUG] The Day the Earth Stood Still
[DEBUG] The Pursuit of Happyness
[DEBUG] Justin Bieber: Never Say Never
[DEBUG] After Earth

[DEBUG] Independence Day
[DEBUG] Men in Black
[DEBUG] Men in Black II
[DEBUG] Hancock
[DEBUG] Shark Tale
[DEBUG] Made in America
[DEBUG] Six Degrees of Separation
[DEBUG] Jersey Girl
[DEBUG] The Legend of Bagger Vance
[DEBUG] Men in Black 3
[DEBUG] Seven Pounds
[DEBUG] Bad Boys II
[DEBUG] Bad Boys 3
[DEBUG] Enemy of the State
[DEBUG] Wild Wild West
[DEBUG] Hitch
[DEBUG] Ali
[DEBUG] I, Robot
[DEBUG] Live 8
[DEBUG] Where The Day Takes You
[DEBUG] Independence Day 3
[DEBUG] I, Robot 2
[DEBUG] The Pursuit of Happyness
[DEBUG] I Am Legend
[DEBUG] Independence Day 2
[DEBUG] After Earth
[DEBUG] Bad Boys
[DEBUG] Partners in Time: The Making of MIB 3
[DEBUG] David Blaine: Real or Magic

[DEBUG] Size: 0

The first part are films starring Jaden Smith, and second are films starring Will Smith, I want only films that have both. Is retainAll() the best method for this kind of work?

JChris
  • 1,638
  • 5
  • 19
  • 37
  • 2
    Have you read the javadoc on retailAll? – Rafael Winterhalter Dec 08 '13 at 01:00
  • `Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection.` Exactly what I want, what's the problem? – JChris Dec 08 '13 at 01:02
  • I tried it with the names. I cannot reproduce this. Are you sure there aren't anything else interfering? – MAV Dec 08 '13 at 01:02
  • 1
    Maybe MAV, I think it's related to the class Film I have. I'll try something different here. – JChris Dec 08 '13 at 01:04
  • From running the code above, I get 2 from `System.out.println(a.size())`, not 0. You must have a bug somewhere else. Please post your whole code, rather than just a snippet, and make sure that the behaviour really IS what you say it is. Otherwise, we all just chase our tails. – Dawood ibn Kareem Dec 08 '13 at 01:12

1 Answers1

11

I suspect that you are storing instances of your own class in the lists and not Strings.

retainAll compares the content using the equals method. If you are storing instances of your own class and that class do not override equals it will compare references. Since you do not have the same instance in both lists (but rather different instances, which contain the same value), it will remove all movies from the first list.

You can prevent this by implementing equals in your class. To do this you can take a look at this answer.

Community
  • 1
  • 1
MAV
  • 7,260
  • 4
  • 30
  • 47
  • Exactly. Totally correct. I just tried the comparison using an ugly for-statement to compare using my `getID()`, just for debug, and it's just as you said. I'll do a override, thanks! – JChris Dec 08 '13 at 01:18
  • I have read hundreds of similar answers to this one and this was the first one that bothered to properly explain the concept. Thank you! – Chris Aug 14 '15 at 08:22