• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

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

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 5458
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41763
887
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41763
887
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41763
887
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are String objects.
 
author
Posts: 23942
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41763
887
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23942
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And hashCode(), of course.
 
Henry Wong
author
Posts: 23942
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5458
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic