Test with Different Element Types
In the previous step, we used the contains() method with a List of String objects. The contains() method is versatile and can be used with collections that store different types of elements, such as numbers, custom objects, or even other collections.
When using contains(), Java relies on the equals() method of the objects within the collection to determine if the element being searched for is a match. For primitive types (like int, double, boolean), their corresponding wrapper classes (Integer, Double, Boolean) are used, and their equals() methods compare the values. For objects, the default equals() method checks if the two object references point to the same memory location. However, many classes (like String, Integer, etc.) override the equals() method to compare the actual content or value of the objects.
Let's modify our program to work with a List of Integer objects and see how contains() behaves.
-
Open the HelloJava.java file in the WebIDE editor.
-
Replace the current code with the following:
import java.util.ArrayList;
import java.util.List;
public class HelloJava {
public static void main(String[] args) {
// Create a List of integers
List<Integer> numbers = new ArrayList<>();
// Add some numbers to the list
numbers.add(10);
numbers.add(25);
numbers.add(5);
numbers.add(50);
// Check if the list contains 25
boolean hasTwentyFive = numbers.contains(25);
System.out.println("Does the list contain 25? " + hasTwentyFive);
// Check if the list contains 100
boolean hasOneHundred = numbers.contains(100);
System.out.println("Does the list contain 100? " + hasOneHundred);
// Check if the list contains the Integer object with value 5
boolean hasFiveObject = numbers.contains(Integer.valueOf(5));
System.out.println("Does the list contain Integer object with value 5? " + hasFiveObject);
}
}
In this code:
- We create a
List that specifically holds Integer objects: List<Integer> numbers = new ArrayList<>();.
- We add integer values to the list using
numbers.add(). Java automatically converts the primitive int values (10, 25, 5, 50) into Integer objects (this is called autoboxing).
- We use
numbers.contains(25) and numbers.contains(100) to check for the presence of integer values. Again, Java autoboxes the primitive int values 25 and 100 into Integer objects before performing the check.
- We also explicitly create an
Integer object using Integer.valueOf(5) and check if the list contains this specific object.
-
Save the file.
-
Compile the program in the Terminal:
javac HelloJava.java
-
Run the program:
java HelloJava
You should see output similar to this:
Does the list contain 25? true
Does the list contain 100? false
Does the list contain Integer object with value 5? true
This demonstrates that contains() works correctly with Integer objects, comparing their values. The contains() method effectively uses the equals() method of the elements in the collection and the element being searched for.
In the next step, we will explore a special case: handling null elements with the contains() method.