TreeSet dalam Java dianggap sebagai salah satu pelaksanaan utama antara muka SortedSet yang membantu dalam storan. Dalam hal ini, unsur-unsur disusun sedemikian rupa sehingga ia ditetapkan dalam susunan atau susunan semula jadi berdasarkan pembanding yang jelas. TreeSet mewarisi kelas AbstractSet dan melaksanakan antara muka NavigableSet. Walaupun ia serupa dengan HashSet, ia mengekalkan pesanan manakala HashSet tidak mengekalkannya. Selain itu, elemen nol dibenarkan oleh TreeSet, tidak seperti HashSet. Butiran lanjut tentang TreeSet akan dibincangkan dalam bahagian berikut.
Mulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Ciri-ciri TreeSet
Di bawah ialah ciri utama TreeSet.
- Nilai pendua tidak dibenarkan.
- Objek diisih berdasarkan tertib menaik.
- Tertib sisipan tidak dipelihara. Selain itu, elemen diisih berdasarkan kekunci.
- Objek heterogen tidak dibenarkan untuk dimasukkan. Jika kita cuba memasukkan, classCastException akan dilemparkan semasa masa jalan.
- Simpan sejumlah besar data yang diisih.
- Masa Dapatkan Cepat.
- Akses Pantas.
- Unsur null tidak dibenarkan.
- Operasi seperti menambah, mencari dan mengalih keluar mengambil masa O(log n).
- Dalam kes operasi seperti mencetak n bilangan elemen, kerumitan masa O(n) akan hadir.
- Tidak disegerakkan.
- Kehadiran TreeMap ada untuk menyimpan elemen.
- Tidak selamat untuk benang.
Sintaks:
Untuk mencipta TreeSet, import pakej java.util.TreeSet dahulu. Selepas itu, buat TreeSet menggunakan sintaks:
TreeSet<Integer> num= new TreeSet<>();
Memandangkan kami telah mencipta TreeSet tanpa menyebut sebarang hujah, elemen akan diisih mengikut susunan semula jadinya. iaitu tertib menaik.
Seperti yang telah disebutkan, pengisihan boleh disesuaikan dengan menggunakan Pembanding antara muka.
Pembina TreeSet di Java
TreeSet dalam Java mempunyai 4 pembina. Mereka ialah:
-
TreeSet(): TreeSet baharu dan kosong akan dibuat dengan pengisihan berdasarkan susunan semula jadi.
-
TreeSet(Collection extends E> c): TreeSet baharu akan dibuat dengan elemen yang disebut dalam koleksi c dan ia akan diisih berdasarkan susunan semula jadi.
-
TreeSet(Comparator super E> comparator): TreeSet baharu dan kosong akan dibuat dengan pengisihan berdasarkan pembanding yang disebutkan.
-
TreeSet(SortedSet s): TreeSet baharu akan dibuat dengan elemen yang disebut dalam sortedset s dengan susunan yang sama dengannya.
Kaedah TreeSet dalam Java
Terdapat beberapa fungsi yang perlu dilakukan dalam TreeSet. Mari kita lihat apa itu.
-
add(Ee): An element e will be added to the set if it is not present in it.
-
addAll(Collection extends E> c): All the elements in collection c will be added to the set.
-
E ceiling(Ee): The last element which is greater than or equal to the element in the set will be returned.
-
clear(): All the elements in the set will be removed.
-
clone(): A shallow copy will be returned for the TreeSet instance.
-
comparator(): The comparator used in the set will be returned. If natural ordering is used, null will be returned.
-
contains(Objecto): If the set contains the element o, true will be returned.
-
descendingIterator(): An iterator will be returned over the elements in descending order.
-
descendingSet(): A reverse order view will be returned for the elements present in the list.
-
first(): he first or the lowest element in the set will be returned.
-
last(): The last or the largest element in the set will be returned.
-
iterator(): n iterator will be returned over the elements in ascending order.
-
lower(Ee): The greatest element will be returned which is strictly small than the element e which is given. If there is no such element, null will be returned.
-
higher(Ee): The smallest element will be returned which is strictly high than the element e which is given. If there is no such element, null will be returned.
-
isEmpty(): True will be returned if no elements are present.
-
size(): The number of elements in the set will be returned. In other words, cardinality will be returned.
-
pollFirst(): The first or the lowest element in the set will be retrieved and removed. If there are no elements in the set, the null will be returned.
-
pollLast(): The last or the highest element in the set will be retrieved and removed. If there are no elements in the set, the null will be returned.
-
remove(Objecto): If the set contains the element o, it will be removed.
-
subSet(EfromElement, boolean fromInclusive, E toElement, boolean toInclusive): A view will be returned for the portion of the set from the range from Element to toElement.
-
subSet(EfromElement, E toElement): A view will be returned for the portion of the set from the range fromElement(inclusive) to toElement(exclusive).
-
tailSet(EfromElement): view will be returned for the portion of the set where elements are larger than fromElement.
-
tailSet(EfromElement, boolean inclusive): A view will be returned for the portion of the set where elements are larger than fromElement. It is considered if inclusive is true.
Example of TreeSet in Java
Java program to create a tree set with the natural ordering of elements.
import java.util.SortedSet;
import java.util.TreeSet;
//class
public class TreeSetExample {
//main method
public static void main(String[] args) {
// TreeSet <u>fam</u> creation
SortedSet<String> fam = new TreeSet<>();
// Adding new elements to a TreeSet
fam.add("Anna");
fam.add("Adam");
fam.add("Sam");
fam.add("Iza");
//print the treeset
System.out.println("Fam Set : " + fam);
// Trying to add duplicate element <u>Anna</u>
fam.add("Anna");
System.out.println("Added element Anna, Now the treeset is : " + fam);
// Trying to add element <u>Anna</u> with lower case
fam.add("anna");
System.out.println("Added element anna, Now the treeset is : " + fam);
}
}
Output:
A treeset fam is created first. Once it is created, elements are added to it and the whole treeset is printed. After this, an element ‘Anna’ which is already present in the treeset is trying to get added. Since duplicates are not allowed in TreeSet, it is not get added. After that, an element ‘anna’ which is the lower case of the already existing element ‘Anna’ is added. Since it is in lowercase, it gets added to the treeset without any problem.
Conclusion
TreeSet in Java is an implementation of the interface SortedSet that helps in storage where the elements are arranged in natural ordering or based on the comparator. Several aspects of TreeSet is discussed in this document.
Atas ialah kandungan terperinci TreeSet dalam Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!