0

I have an ArrayList in Java, which contains Objects, these Objects are made of a string name and a number.

I want to check if any of the Objects is twice in the list and then update the number of one of them, afterwards delete the other one, so it wont get counted another time.

I already know how it works, but i have a big problem, both methodes, "contains" and "remove" do not work with the objects i want to work with. For expamle i want to remove the first object which contains the name XXXX, it wont remove the thing at all. It seems like these two methods are not made for selfmade Objects.

Is there a way of overriding the remove and contains method ? Eclipse does not help here with the autogenerated stuff.

btw: i have defiened an own "equals" method for the objects in my list which is working without any problems ( i always thought the arraylist methods would make use of the object methods )..

thanks.

user1203092
  • 41
  • 1
  • 7
  • 4
    A snippet of code is worth a thousand words – adarshr Feb 10 '12 at 22:42
  • 1
    When you define equals method you need to define valid hashcode implementation also. I strongly suspect you are missing hashcode part. Post related code. – kosa Feb 10 '12 at 22:43
  • Could you post your code where ArrayList fails ? – Shankar Narayana Damodaran Feb 10 '12 at 22:43
  • Just checking, but you're using java.util.ArrayList, right? Not your own ArrayList? If you are, this needs to be a totally different discussion. Also, as previously mentioned, please paste your code in. If we can see what's going on, we can help. If we can't see, we can only speculate. – Ryan Amos Feb 10 '12 at 22:51

1 Answers1

3

equals() and hashCode() methods should always be provided in tandem.

Look into What issues should be considered when overriding equals and hashCode in Java?

Community
  • 1
  • 1
Yuriy Zubarev
  • 2,821
  • 18
  • 24