手動ループで検証する
前のステップでは、部分集合をチェックするために便利な containsAll()
メソッドを使用しました。containsAll()
は効率的ですが、ループを使用して手動でこのチェックを行う方法を理解することは有益です。これにより、コレクションメソッドが内部でどのように動作するかについての理解が深まります。
SubsetCheck.java
ファイルに、部分集合のチェックを手動で行う新しいメソッドを追加しましょう。WebIDE エディタで ~/project/SubsetCheck.java
を開きます。
SubsetCheck
クラス内で、main
メソッドの外に以下のメソッドを追加します。
// 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;
}
この新しいメソッド isSubsetManual
は、2 つのリストを入力として受け取ります。そして、subList
内の各 element
をループで処理します。ループ内では、contains()
メソッドを使用して、mainList
が現在の element
を含んでいるかどうかをチェックします。subList
内に mainList
に含まれない要素が 1 つでも見つかった場合、すぐに subList
は部分集合ではないと判断し、false
を返します。ループが終了しても subList
内に mainList
に欠けている要素が見つからない場合、すべての要素が存在することを意味し、メソッドは true
を返します。
次に、main
メソッドからこの新しいメソッドを呼び出し、その結果を containsAll()
の結果と比較しましょう。SubsetCheck.java
の main
メソッドを変更して、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;
}
}
変更した SubsetCheck.java
ファイルを保存します。
次に、ターミナルで更新されたコードをコンパイルします。
javac SubsetCheck.java
そして、プログラムを再度実行します。
java SubsetCheck
以下のような出力が表示され、containsAll()
メソッドと手動ループメソッドの両方が同じ結果を生成することが示されます。
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
このステップでは、潜在的な部分集合を繰り返し処理し、メインリスト内に各要素が存在するかどうかをチェックすることで、containsAll()
と同じ結果を得ることができることを示しています。containsAll()
は、その簡潔さと Java ライブラリ内の潜在的なパフォーマンス最適化のため、一般的に推奨されますが、手動アプローチを理解することは学習にとって価値があります。