Überprüfung mit einer manuellen Schleife
Im vorherigen Schritt haben wir die bequeme containsAll()
-Methode verwendet, um zu prüfen, ob eine Liste eine Teilmenge einer anderen ist. Obwohl containsAll()
effizient ist, ist es hilfreich, zu verstehen, wie man diese Prüfung manuell mit einer Schleife durchführt. Dies vertieft Ihr Verständnis davon, wie Sammlungsmethoden intern funktionieren könnten.
Fügen wir eine neue Methode zu unserer SubsetCheck.java
-Datei hinzu, um die Teilmengengeprüfung manuell durchzuführen. Öffnen Sie ~/project/SubsetCheck.java
im WebIDE-Editor.
Fügen Sie die folgende Methode innerhalb der SubsetCheck
-Klasse, aber außerhalb der main
-Methode hinzu:
// 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;
}
Diese neue Methode isSubsetManual
nimmt zwei Listen als Eingabe. Sie durchläuft dann jedes element
in der subList
. Innerhalb der Schleife prüft sie, ob die mainList
das aktuelle element
mithilfe der contains()
-Methode enthält. Wenn sie auch nur ein Element in subList
findet, das nicht in mainList
enthalten ist, weiß sie sofort, dass subList
keine Teilmenge ist und gibt false
zurück. Wenn die Schleife ohne das Finden eines fehlenden Elements in subList
beendet wird, bedeutet dies, dass alle Elemente vorhanden sind, und die Methode gibt true
zurück.
Rufen wir jetzt diese neue Methode aus unserer main
-Methode auf, um ihr Ergebnis mit containsAll()
zu vergleichen. Modifizieren Sie die main
-Methode in SubsetCheck.java
, um Aufrufe von isSubsetManual
einzubeziehen:
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;
}
}
Speichern Sie die modifizierte SubsetCheck.java
-Datei.
Kompilieren Sie jetzt den aktualisierten Code im Terminal:
javac SubsetCheck.java
Und führen Sie das Programm erneut aus:
java SubsetCheck
Sie sollten eine Ausgabe ähnlich der folgenden sehen, die zeigt, dass sowohl die containsAll()
-Methode als auch unsere manuelle Schleifenmethode die gleichen Ergebnisse liefern:
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
Dieser Schritt zeigt, dass Sie dasselbe Ergebnis wie mit containsAll()
erzielen können, indem Sie die potenzielle Teilmenge durchlaufen und die Anwesenheit jedes Elements in der Hauptliste prüfen. Obwohl containsAll()
aufgrund seiner Kürze und möglicher Leistungsoptimierungen in der Java-Bibliothek im Allgemeinen bevorzugt wird, ist das Verständnis des manuellen Ansatzes für das Lernen von Wert.