Trong hướng dẫn này, chúng ta sẽ tìm hiểu về giao diện Set trong Java và các phương thức của nó.
Các Set
giao diện của Java Collections
framework cung cấp các tính năng của bộ toán học trong Java. Nó mở rộng Collection
giao diện.
Không giống như List
giao diện, các tập hợp không thể chứa các phần tử trùng lặp.
Các lớp triển khai Set
Vì Set
là một giao diện nên chúng ta không thể tạo các đối tượng từ nó.
Để sử dụng các chức năng của Set
giao diện, chúng ta có thể sử dụng các lớp sau:
- HashSet
- LinkedHashSet
- EnumSet
- TreeSet
Các lớp này được định nghĩa trong Collections
khuôn khổ và triển khai Set
giao diện.
Các giao diện mở rộng Bộ
Các Set
giao diện cũng được mở rộng bởi những subinterface:
- SortedSet
- NavigableSet
Làm thế nào để sử dụng Set?
Trong Java, chúng ta phải nhập java.util.Set
gói để sử dụng Set
.
// Set implementation using HashSet
Set<String> animals = new HashSet<>();
Ở đây, chúng tôi đã tạo ra một con vậtSet
được gọi là . Chúng tôi đã sử dụng lớp để triển khai giao diện.HashSet
Set
Phương pháp đặt
Các Set
giao diện bao gồm tất cả các phương pháp của Collection
giao diện. Đó là vì Collection
là một giao diện siêu của Set
.
Một số phương pháp thường được sử dụng của Collection
giao diện cũng có sẵn trong Set
giao diện là:
- add() – adds the specified element to the set
- addAll() – adds all the elements of the specified collection to the set
- iterator() – returns an iterator that can be used to access elements of the set sequentially
- remove() – removes the specified element from the set
- removeAll() – removes all the elements from the set that is present in another specified set
- retainAll() – retains all the elements in the set that are also present in another specified set
- clear() – removes all the elements from the set
- size() – returns the length (number of elements) of the set
- toArray() – returns an array containing all the elements of the set
- contains() – returns
true
if the set contains the specified element - containsAll() – returns
true
if the set contains all the elements of the specified collection - hashCode() – returns a hash code value (address of the element in the set)
Để tìm hiểu thêm về các phương thức của Set
giao diện, hãy truy cập Bộ Java (tài liệu Java chính thức) .
Đặt hoạt động
Set
Giao diện Java cho phép chúng ta thực hiện các phép toán tập hợp toán học cơ bản như liên hiệp, giao điểm và tập hợp con.
- Union – to get the union of two sets x and y, we can use
x.addAll(y)
- Intersection – to get the intersection of two sets 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)
Triển khai Giao diện Đặt
1. Triển khai lớp HashSet
import java.util.Set;
import java.util.HashSet;
class Main {
public static void main(String[] args) {
// Creating a set using the HashSet class
Set<Integer> set1 = new HashSet<>();
// Add elements to the set1
set1.add(2);
set1.add(3);
System.out.println("Set1: " + set1);
// Creating another set using the HashSet class
Set<Integer> set2 = new HashSet<>();
// Add elements
set2.add(1);
set2.add(2);
System.out.println("Set2: " + set2);
// Union of two sets
set2.addAll(set1);
System.out.println("Union is: " + set2);
}
}
Đầu ra
Set1: [2, 3] Set2: [1, 2] Union is: [1, 2, 3]
Để tìm hiểu thêm về HashSet
, hãy truy cập Java HashSet .
2. Triển khai lớp TreeSet
import java.util.Set;
import java.util.TreeSet;
import java.util.Iterator;
class Main {
public static void main(String[] args) {
// Creating a set using the TreeSet class
Set<Integer> numbers = new TreeSet<>();
// Add elements to the set
numbers.add(2);
numbers.add(3);
numbers.add(1);
System.out.println("Set using TreeSet: " + numbers);
// Access Elements using iterator()
System.out.print("Accessing elements using iterator(): ");
Iterator<Integer> iterate = numbers.iterator();
while(iterate.hasNext()) {
System.out.print(iterate.next());
System.out.print(", ");
}
}
}
Đầu ra
Set using TreeSet: [1, 2, 3] Accessing elements using iterator(): 1, 2, 3,
Để tìm hiểu thêm TreeSet
, hãy truy cập Java TreeSet .
Bây giờ chúng ta biết những gì Set
là, chúng ta sẽ thấy hiện thực của nó trong các lớp học như EnumSet
, HashSet
, LinkedHashSet
và TreeSet
trong các hướng dẫn tiếp theo.