Collection Flashcards
Collection
-interface implemented by all collections
Collections
-class with static methods used to manipulate collections
Ordering
- how an element is stored
- determines where an element is in a collection.For example, a List stores elements with an index, like an array.
Sorting
-determines an elements position based on the elements value.For example, a SortedSet maintains order by comparisons made according to a supplied rule.Your sorting rule might be alphabetical order, or age, or some other system you define.
HashSet
- Unsorted
- no duplicates
TreeSet
- Sorted
- no duplicates
When to used collections?
For instance, if you needed a container that had only unique elements then a Set would be your preferred choice.If you don’t require all elements to be unique then a List might be better.A Map is great for managing associations between information such as keeping a username associated with an id.If order matters in your collection, typically you would use a sorted version of your container such as a TreeMap or TreeSet.But If speed is more important, you would use the unsorted version such as HashMap or HashSet.
Maps
- Map interface does not extend the collection interface
- stores data in key-value pairs
- sometimes called “Dictionaries”
Map Implementations
- HashTable and HashMap use hash codes to uniquely identify contents
- hashing functions are complicated equations defined in Object.hashCode()
Differences between HashTable and HashMap
-One of the most important differences between Hashtable and HashMap, is that Hashtable is synchronized - meaning that in applications that use multiple concurrent processes or threads, a Hashtable can be accessed safely by two different processes at the same time.HashMap is not synchronized, but it is faster and preferred for applications where only one process needs to access the map at a time.Also, a Hashtable does not allow any null keys or values whereas a HashMap will allow up to one null key and any value can be null
TreeMap
similar to hash map but its elements are sorted
Map Methods
-maps can access contents in O(1). put get remove keySet Values