Perché i metodi treeMap.entrySet() e treeMap.keySet() non restituiscono SortedSet? Potrei andare così lontano da dire che è un errore. Come per l'API, un Set è definito come non avendo un particolare ordine. Tuttavia, i set restituiti da TreeMap do hanno un ordine particolare.Non dovrebbe treeMap.entrySet() restituire un SortedSet?
8
A
risposta
10
Penso che se hanno scritto l'interfaccia SortedMap
ora, sia keySet
che entrySet
restituire SortedSet
. Tuttavia, l'interfaccia SortedMap
è stata introdotta in Java 1.2 prima che i tipi di ritorno covarianti fossero consentiti. Non possono modificarlo ora poiché ci saranno implementazioni di SortedMap
per le quali keySet
e entrySet
restituiscono un Set
ovvero non a SortedSet
.
+0
Anche se non è un SortedSet, in effetti definisce che il Set restituito è in ordine ascendente, come definito nel javadoc dell'interfaccia SortedMap. – Robin
Problemi correlati
- 1. Python: perché isinstance può restituire False, quando dovrebbe restituire True?
- 2. Perché dovrebbe restituire NaN in MATLAB
- 3. divisione MATLAB ... dovrebbe 29/128 restituire 0?
- 4. Perché l'operatore di assegnazione dovrebbe restituire un riferimento all'oggetto?
- 5. Un MAMP dovrebbe restituire :: 1 come IP su localhost?
- 6. Java: il metodo compareTo modificato dice che deve restituire un int, ma dovrebbe restituire uno
- 7. Una richiesta HTTP OPTIONS può restituire un 204 o dovrebbe sempre restituire 200?
- 8. Perché TreeSet restituisce il valore SortedSet
- 9. Il codice gestito dovrebbe restituire un errore o generare eccezioni al codice non gestito?
- 10. Scala SortedSet - ordinati per un ordine e unici per qualcos'altro?
- 11. Come aggiungere elementi SortedSet da una matrice?
- 12. FindFirst dovrebbe restituire i file trovati in ordine alfabetico?
- 13. Quale interfaccia dovrebbe restituire il mio servizio? IQueryable, IList, IEnumerable?
- 14. La mia funzione dovrebbe restituire un puntatore a std :: vector o un riferimento a std :: vector?
- 15. Come restituire un flusso da un metodo, sapendo che dovrebbe essere eliminato?
- 16. GCC - non dovrebbe essere emesso un avviso?
- 17. IAuthenticationRequest.RedirectToProvider non dovrebbe tornare, ma lo fa
- 18. blocco waitpid quando non dovrebbe
- 19. Non dovrebbe istream :: peek() restituire sempre ciò che hai appena messo()?
- 20. Che cosa dovrebbe restituire un factory iterator lua in caso di iterate
- 21. Restituire non in funzione
- 22. Non dovrebbe causare un Overflow? Non è così!
- 23. Cosa getter e setter dovrebbe e non dovrebbe fare
- 24. Quando dovrebbe essere una classe .NET Override()? Quando non dovrebbe?
- 25. Cosa dovrebbe essere OO e cosa non dovrebbe?
- 26. SortedSet <T> vs HashSet <T>
- 27. Cosa dovrebbe e non dovrebbe essere in un file di intestazione Erlang (.hrl)?
- 28. SortedSet <T> e anonymous IComparer <T> nel costruttore non funziona
- 29. restituire un oggetto Void
- 30. Restituire un vuoto?
Cosa ti fa pensare che non restituisca un 'SortedSet' (credo che tu intendessi' TreeSet' lì)? –
@RohitJain Questo è ciò che dice l'API. L'API –
dice solo che restituirebbe la vista 'Set'. Ciò che l'effettiva implementazione di 'Set' restituisce non è lì, e non dovrebbe esserci. Questo è il dettaglio dell'implementazione. –