• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to check if one ArrayList contains objects stores in another ArrayList  RSS feed

 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two ArrayLists and I want to check if the items stored in one list is also in the other and if it is, store it in a third ArrayList. My problem is that each object from list two is getting stored in list three.

The lists contain:

List one: 50 items
List two: 15 items

Of the 15 items in list two, 10 of them are in List one and these are the ten that I want to store in List three.

Here is the code I have came up with but something is not right. I didn't see it necessary to show the rest of the code since I know storing the objects in list one and two is working properly.

 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Raymond,

your method should work, if you can get your indices right.

But there is an easier way. For your intended third List, have a
look at the methods 'addAll' and 'retainAll', in the Collection API.

Greetz,
Piet
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This code adds the item to listThree unconditionally. What changes is that it inserts it at index 0 or index 1 depending on whether it is in listOne. That's not what you want. Try creating an if statement around the add call.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet,
addAll() would allow duplicates in the list. If this were a set, I see how that would work. I'm puzzled for a list.
 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Piet,
addAll() would allow duplicates in the list. If this were a set, I see how that would work. I'm puzzled for a list.


How does a set work?
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set is a collection that holds elements and it does not allow duplicates.
However it does not (generally) promise you that it will keep them in any particular order.
Some implementations of Set interface do promise you that like LinkedHashSet.

See this.
 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
This code adds the item to listThree unconditionally. What changes is that it inserts it at index 0 or index 1 depending on whether it is in listOne. That's not what you want. Try creating an if statement around the add call.


I tried this and got the same outcome. It still adds every object from list two into list three

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type of objects are in the list? Do they implement equals()?
 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are String objects.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raymond Gillespie wrote:They are String objects.


Your code shows them as Item objects -- see your previous post...

Raymond Gillespie wrote:
I tried this and got the same outcome. It still adds every object from list two into list three



Henry
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not what I meant. They appear to be the Items class from your for loop. Does the Items class have an equals() method? (If not, it needs one for this to work). Can you show the implementation of that method?
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raymond Gillespie wrote:They are String objects.

But your for loops says, it's Items.
 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not have the equals() method in my Items class and that was the issue. It works fine now. Thanks!!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raymond Gillespie wrote:It is an items object. What I mean is that Strings are in the objects. I do have an equals() method in the items class so perhaps that is the issue.


Whether it is an issue or not would depend... well, perhaps you can show us the implementation of the equals() method from the Items class.

Henry
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And hashCode(), of course.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:And hashCode(), of course.


Well, IMO, it *should* have a corresponding hashCode() method, but it is not needed here. The code is only using array lists, and not a hashing collection.

Henry

 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also added the hasCode() method as well.
 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Raymond Gillespie wrote:It is an items object. What I mean is that Strings are in the objects. I do have an equals() method in the items class so perhaps that is the issue.


Whether it is an issue or not would depend... well, perhaps you can show us the implementation of the equals() method from the Items class.

Henry


This is the equals() method. I didn't write it. I just used the generate feature in Eclipse.


 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Piet,
addAll() would allow duplicates in the list. If this were a set, I see how that would work. I'm puzzled for a list.

Sorry, I missed this reply.

The opening post did not mention anything about duplicates or not. So what I suggested
simply mirrors what was in the arrays already.

But anyway, problem is solved now, I didn't know that today's IDE's are able
to generate an 'equals' method (I knew they can generate a hashcode).

Greetz,
Piet
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raymond Gillespie wrote:This is the equals() method. I didn't write it. I just used the generate feature in Eclipse.

Well, it's produced a lot of redundant code because, assuming your 'item' field's equals() method is written the same way (or uses Object's), the second null check is not needed, viz:I also dislike "class-based" equals() methods intensely, because they violate LSP by definition.
The following:will work just as well, and doesn't require annotation.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!