Verify with Manual Loop
In the previous step, we used the convenient containsAll()
method to check for a subset. While containsAll()
is efficient, it's helpful to understand how you would perform this check manually using a loop. This will deepen your understanding of how collection methods might work internally.
Let's add a new method to our SubsetCheck.java
file to perform the subset check manually. Open ~/project/SubsetCheck.java
in the WebIDE editor.
Add the following method inside the SubsetCheck
class, but outside the main
method:
// Method to manually check if subList is a subset of mainList
public static boolean isSubsetManual(List<String> mainList, List<String> subList) {
// Iterate through each element in the subList
for (String element : subList) {
// If the mainList does NOT contain the current element from subList,
// then subList is not a subset, and we can return false immediately.
if (!mainList.contains(element)) {
return false;
}
}
// If we have checked all elements in subList and found them all in mainList,
// then subList is a subset.
return true;
}
This new method isSubsetManual
takes two lists as input. It then loops through each element
in the subList
. Inside the loop, it checks if the mainList
contains the current element
using the contains()
method. If it finds even one element in subList
that is not in mainList
, it immediately knows that subList
is not a subset and returns false
. If the loop finishes without finding any element in subList
that is missing from mainList
, it means all elements are present, and the method returns true
.
Now, let's call this new method from our main
method to compare its result with containsAll()
. Modify the main
method in SubsetCheck.java
to include calls to isSubsetManual
:
import java.util.ArrayList;
import java.util.List;
public class SubsetCheck {
public static void main(String[] args) {
// Create the main list
List<String> mainList = new ArrayList<>();
mainList.add("Apple");
mainList.add("Banana");
mainList.add("Cherry");
mainList.add("Date");
// Create a potential subset list
List<String> subList = new ArrayList<>();
subList.add("Banana");
subList.add("Cherry");
// Check if subList is a subset of mainList using containsAll()
boolean isSubsetContainsAll = mainList.containsAll(subList);
// Check if subList is a subset of mainList using manual loop
boolean isSubsetManualCheck = isSubsetManual(mainList, subList);
// Print the result
System.out.println("Main List: " + mainList);
System.out.println("Sub List: " + subList);
System.out.println("Is subList a subset of mainList (containsAll)? " + isSubsetContainsAll);
System.out.println("Is subList a subset of mainList (manual check)? " + isSubsetManualCheck);
// Create another list that is not a subset
List<String> anotherList = new ArrayList<>();
anotherList.add("Banana");
anotherList.add("Grape"); // Grape is not in mainList
// Check if anotherList is a subset of mainList using containsAll()
boolean isAnotherSubsetContainsAll = mainList.containsAll(anotherList);
// Check if anotherList is a subset of mainList using manual loop
boolean isAnotherSubsetManualCheck = isSubsetManual(mainList, anotherList);
// Print the result for the second check
System.out.println("\nAnother List: " + anotherList);
System.out.println("Is anotherList a subset of mainList (containsAll)? " + isAnotherSubsetContainsAll);
System.out.println("Is anotherList a subset of mainList (manual check)? " + isAnotherSubsetManualCheck);
}
// Method to manually check if subList is a subset of mainList
public static boolean isSubsetManual(List<String> mainList, List<String> subList) {
// Iterate through each element in the subList
for (String element : subList) {
// If the mainList does NOT contain the current element from subList,
// then subList is not a subset, and we can return false immediately.
if (!mainList.contains(element)) {
return false;
}
}
// If we have checked all elements in subList and found them all in mainList,
// then subList is a subset.
return true;
}
}
Save the modified SubsetCheck.java
file.
Now, compile the updated code in the Terminal:
javac SubsetCheck.java
And run the program again:
java SubsetCheck
You should see output similar to this, showing that both the containsAll()
method and our manual loop method produce the same results:
Main List: [Apple, Banana, Cherry, Date]
Sub List: [Banana, Cherry]
Is subList a subset of mainList (containsAll)? true
Is subList a subset of mainList (manual check)? true
Another List: [Banana, Grape]
Is anotherList a subset of mainList (containsAll)? false
Is anotherList a subset of mainList (manual check)? false
This step demonstrates that you can achieve the same result as containsAll()
by iterating through the potential subset and checking for the presence of each element in the main list. While containsAll()
is generally preferred for its conciseness and potential performance optimizations in the Java library, understanding the manual approach is valuable for learning.