[Java]How to check if one ArrayList contains objects stores in another

  • Java
  • Thread starter sinhasiha
  • Start date
In summary: If the objects are not the same object, then they are not equal. If you want to check if two objects have the same properties, then you need to override the .Equals method and specify what makes two objects "equal".
  • #1
sinhasiha
2
0
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.

view plainprint?
Note: Text content in the code blocks is automatically word-wrapped
Java:
for (Items item : listTwo){
listThree.add(listOne.contains(item) ? 1 : 0, item);

}



¸.•°*”˜˜”*°•. ¸.•°*”˜˜”*°•.¸
..¸.•°*”˜˜”*°•Thank you.•°*”˜˜”*°•.¸

————————————————
 
Last edited:
Technology news on Phys.org
  • #2
You are misunderstanding what the "," operand does. The expression "listOne.contains(item) ? 1 : 0, item" first evaluates "listOne.contains(item) ? 1 : 0" giving either 1 or 0, then throws away that result, then evaluates "item", which is just a reference to "item". So apart from wasting a bit of time checking in the item is in listOne, your code does the same thing as "listThree.add(item)", which of course always adds "item" to listThree.

If you really want to use the "? :" operator, you could write something like
Code:
listOne.contains(item) ? listThree.add(item) : 0;
but most people would just write
Code:
if (listOne.contains(item)) 
   listThree.add(item);
 
  • #3
Thank you
 
  • #4
AlephZero said:
The expression "listOne.contains(item) ? 1 : 0, item" first evaluates "listOne.contains(item) ? 1 : 0" giving either 1 or 0, then throws away that result, then evaluates "item", which is just a reference to "item".

Java does not have a comma operator like C. Instead, what happens is that the add(int,E) method is invoked, that is, the first argument in the invocation will be either 0 or 1 depending on whether the item from the second list exist in the first list or not. Inserting in position 0 or 1 means the item will be insert first or second respectively in the list. If the first item in the second list is contained in the first list, then add is invoked with index 1 on an empty list which would give an IndexOutOfBounds exception.
 
  • #5
If the "items" stored in your ArrayLists are objects, you can use .Equals to test for "equality", which really checks the memory address.
 

What is an ArrayList?

An ArrayList in Java is a dynamic data structure that allows the storage of multiple objects in a single variable. It can grow or shrink in size as needed and can hold any type of object.

How do I check if one ArrayList contains objects stored in another?

To check if one ArrayList contains objects stored in another, you can use the containsAll() method. This method takes in another ArrayList as a parameter and returns true if all the objects in the second ArrayList are present in the first one, otherwise it returns false.

Can I use the equals() method to check if one ArrayList contains objects stored in another?

No, the equals() method cannot be used to check if one ArrayList contains objects stored in another. This method only compares the references of the objects, not their actual values. Therefore, it will return false even if the two ArrayLists have the same objects.

What is the time complexity of the containsAll() method?

The time complexity of the containsAll() method is O(n), where n is the size of the ArrayList being checked against. This means that the time it takes to check if one ArrayList contains objects stored in another increases linearly with the size of the ArrayList.

Can I use the contains() method to check if an ArrayList contains another ArrayList?

No, the contains() method can only be used to check if a single object is present in an ArrayList. It cannot be used to check if one ArrayList contains another ArrayList.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • General Discussion
Replies
1
Views
869
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top