English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In diesem Tutorial werden wir das Set-Interface und seine Methoden in Java lernen.
Das Set-Interface der Java Collections-Framework bietet die Funktionen mathematischer Sammlungen in Java. Es erbt das Collection-Interface.
Im Gegensatz zum List-Interface können Set-Sammlungen keine wiederholten Elemente enthalten.
Da Set ein Interface ist, kann aus ihm kein Objekt erstellt werden.
Um die Funktionen des Set-Interfaces zu nutzen, können wir die folgenden Klassen verwenden:
Diese Klassen sind im Collections-Framework definiert und implementieren das Set-Interface.
Diese Set-Schnittstelle erweitert auch diese Unter-Schnittstellen:
In Java muss das Paket java.util.Set importiert werden, um Set verwenden zu können.
//Set mit HashSet implementieren Set<String> animals = new HashSet<>();
Hier haben wir eine Set namens animals erstellt. Wir haben HashSet die Klasse verwendet, um das Set-Interface zu implementieren.
Die Set-Schnittstelle enthält alle Methoden der Collection-Schnittstelle. Dies liegt daran, dass Collection das Super-Interface von Set ist.
Die Set-Schnittstelle bietet auch einige häufig verwendete Methoden der Collection-Schnittstelle:
add() - Das angegebene Element zur Sammlung hinzufügen
addAll() - Alle Elemente der angegebenen Sammlung zur Sammlung hinzufügen
iterator() -Einen Iterator zurückgeben, der zur sequenziellen Durchlauf der Elemente in der Sammlung verwendet werden kann
remove() - Aus der Sammlung das angegebene Element entfernen
removeAll() - Remove all elements from the collection that exist in another specified collection
keepAll() -Keep all elements that still exist in another specified collection
clear() - Remove all elements from the collection
size() - Return the length of the collection (number of elements)
toArray() - Return an array containing all elements of the collection
contains() - Return true if the collection contains the specified element
containsAll() - Return true if the collection contains all elements of the specified collection
hashCode() -Return the hash code value (the address of the elements in the collection)
The Java Set interface allows us to perform basic mathematical set operations, such as union, intersection, and subset.
Union - To get the union of two collections x and y, we can use x.addAll(y)
Intersection - To get the intersection of two collections x and y, we can use x.retainAll(y)
Subset - To check if x is a subset of y, we can use y.containsAll(x)
1. implement HashSet class
import java.util.Set; import java.util.HashSet; class Main { public static void main(String[] args) { //Create a collection using the HashSet class Set<Integer> set1 = new HashSet<>(); //Add elements to set1 set1.add(2); set1.add(3); System.out.println("Set1: " + set1); //Create another collection using the HashSet class Set<Integer> set2 = new HashSet<>(); //Add element set2.add(1); set2.add(2); System.out.println("Set2: " + set2); //Union of two collections set2.addAll(set1); System.out.println("The union is: " + set2); } }
Ausgaberesultat
Set1: [2, 3] Set2: [1, 2] The union is: [1, 2, 3]
For more information about HashSet, please visitJava HashSet.
2. implement TreeSet class
import java.util.Set; import java.util.TreeSet; import java.util.Iterator; class Main { public static void main(String[] args) { //Create a collection using the TreeSet class Set<Integer> numbers = new TreeSet<>(); // Add elements to the set collection numbers.add(2); numbers.add(3); numbers.add(1); System.out.println("TreeSet: " + numbers); //Access elements using iterator() System.out.print("Mit iterator() Elemente erreichen: "); Iterator<Integer> iterate = numbers.iterator(); while(iterate.hasNext()) { System.out.print(iterate.next()); System.out.print(", "); } } }
Ausgaberesultat
TreeSet: [1, 2, 3] Elemente mit iterator() erreichen: 1, 2, 3,
Für weitere Informationen über TreeSet besuchen SieJava TreeSet.
Jetzt wissen wir, was Set ist, in den nächsten Tutorials werden wir seine Implementierung in Klassen wie EnumSet, HashSet, LinkedHashSet und TreeSet sehen.