How do you choose the right collection for specific cases? I'm sure many of us have asked that question at least once. Let me try to help you to make the right choice:
List
, Set
(such as LinkedHasSet
), and Map
(such as LinkedHashMap
) classes are perfect choices for general purposes. They have enough functionality to cover all of your needs.Here is a table that combines all the classes with the supported features:
Class |
Order |
Sort |
Random access |
Key-values |
Duplicates |
Null |
---|---|---|---|---|---|---|
|
Yes |
Yes |
Yes |
No |
Yes |
Yes |
|
Yes |
No |
No |
No |
Yes |
Yes |
|
Yes |
No |
No |
No |
No |
No |
|
No |
No |
No |
No |
No |
No |
|
Yes |
Yes |
No |
No |
No |
No |
|
Yes |
Yes |
No |
No |
Yes |
Yes |
|
Yes |
No |
Yes |
Yes |
No |
Yes |
|
No |
No |
Yes |
Yes |
No |
Yes |
|
Yes |
Yes |
Yes |
Yes |
No |
Yes |
The order is supported via iteration. Sort is supported via a collection-compare function via a Comparator or an object-compare method via the Comparable interface.
13.59.79.176