Collections and Generics
Java collections
Collection classes
Collection interfaces
Interface | Description |
---|---|
Collection | Defines the basic methods available for all collections. |
Set | Defines a type of collection in which no duplicate elements are allowed. |
List | Defines a type of collection that maintains the order in which elements were added to the list. |
Map | Defines a map, which holds one or more key-value pairs, each consisting of: (1) a key that uniquely identifies an entry and (2) a value that provides data for that key. |
Common collection classes
Class | Description |
---|---|
ArrayList | Works much like an array, but can be easily expanded to accommodate new elements. Very efficient for accessing elements in random sequence, but inserting elements into the middle of the list can be inefficient. |
LinkedList | Similar to an array list, but with more features. Less efficient than an array list for random access, but more efficient when inserting items into the middle of the list. |
HashSet | A collection that stores a set of unique values based on a hash code. Duplicates are not allowed. Objects you add to a hash set must implement a method called hashCode to generate a hash code for the object, which is used to ensure uniqueness. |
HashMap | Similar to a hash set, but is based on the Map interface rather than the Set interface. As a result, a hash map stores key-value pairs whose keys must be unique. |
TreeMap | Stores key-value pairs in a special arrangement called a tree. Entries are automatically maintained in key sequence. |