English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
HashSet e TreeSet appartengono alla struttura delle collezioni. HashSet è un'implementazione dell'interfaccia Set, mentre TreeSet implementano set ordinati. La TreeSet è supportata da TreeMap, mentre HashSet è supportata da HashMap.
Numero | Chiave | HashSet | TreeSet |
---|---|---|---|
1 | Implementazione | La HashSet è implementata utilizzando HashTable | La TreeSet è implementata utilizzando una struttura ad albero. |
2 | Oggetto null | La HashSet permette un oggetto null | La TreeSet non permette oggetti null. Lancia un'eccezione di puntatore nullo. |
3 | Metodo | La HashSet confronta due oggetti utilizzando il metodo equals | La TreeSet confronta due oggetti utilizzando il metodo di confronto. |
4 | Oggetti eterogenei | La HashSet non permette oggetti eterogenei | La TreeSet permette oggetti eterogenei |
5 | Ordina | HashSet non mantiene alcun ordine | TreeSet mantiene l'ordine degli oggetti |
class TreeSetExmaple { public static void main(String[] args){ TreeSet<String> treeset = new TreeSet<String>(); treeset.add("Buono"); treeset.add("For"); treeset.add("Salute"); //Aggiungi elemento duplicato treeset.add("Buono"); System.out.println("TreeSet: "); for(String temp : treeset) { System.out.println(temp); } } }
Risultato di output
TreeSet: Salute For Buono
class HashSetExample { public static void main(String[] args){ HashSet<String> hashSet = new HashSet<String>(); hashSet.add("Buono"); hashSet.add("For"); hashSet.add("Salute"); //Aggiungi elemento duplicato hashSet.add("Buono"); System.out.println("HashSet: "); for(String temp : hashSet) { System.out.println(temp); } } }
Risultato di output
TreeSet: Salute Buono For