Collections part 2 Flashcards
Set characteristics
- Elements are not ordered.
- Elements in the set must be unique.
- Adding a duplicate does nothing.
- Cannot access elements by index, only by iterator (for each).
- add( x ) adds an element to the set.
Set syntax
Set variable = new HashSet(); Set - Set interface - Data Type the Set will Hold HashSet - implementation class
Set order
The Order of the elements in a Set can be controlled by the Implementation Class used.
HashSet - implementation class of Set
- Does not guarantee Order.
2. Allows null.
LinkedHashSet - implementation class of Set
- Order of Insertion
2. Allows null.
TreeSet - implementation class of Set
- Natural Order of Data Type
- Does not allow null
Natural Order of numeric types is numeric order:
1, 2, 10, 20, 21, 30
Natural Order of String is alphanumeric order:
1, 10, 2, 20, 21, 30
Key-Value pairs
A set of 2 pieces of data, where the value is associated by a unique key, allowing the value to be retrieved by providing the key.
Map
A collection that utilizes Key-Value pairs, allowing values to be assigned and then located using user-defined keys.
Map keys characteristics
- Can be any reference type.
- Must be unique.
- Cannot be null.
- Stored as a Set.
Map values characteristics
- Can be any reference type.
- Can have duplicates.
- Can be null.
Map syntax
Map variable = new HashMap();
Map - is an interface
K - the first Type is the Data Type of the Key
V - the second Type is the Data Type of the Value
HashMap - the implementation class to instantiate
Map Methods
put( key, value ), get( key ), remove( key ), containsKey( key ), containsValue( value ), keySet(), entrySet()
Algorithm Problems
Algorithms are not measured by the speed they run, but by how their performance increases as the problem size increases.
Big O Complexities
O(1) - Constant-time
- The complexity doesn’t increase as the problem increases (selecting an item from an Array by index).
O(log n) - Logarithmic
- The complexity increases at a constant factor that is not directly related to the problem size.
O(n) - Linear
- Doubling the problem size, doubles the complexity (iterating over every item in a list).
O(n log n) - Linearithmic
- Doubling the problem increases the complexity by a fixed size (most search algorithms).
O(n2) - Quadratic
- Doubling the problem size multiplies the complexity by a factor of 4 (loop within a loop)
O(nx) - Polynomial
- Double the size of the problem multiplies the complexity by a factor multiplied by the exponent
O(2n) - Exponential
- Increasing the problem size by 1 double the complexity. Doubling the problem size squares the work.
O(n!) - Factorial
Something has gone horribly wrong
Collection Complexity
Each collection time has a complexity associated with
Insert (at the end, at the beginning, at the end), Searching, Retrieval, Removal (from the end, from the beginning, from the end)