Chapter 7: Generics and Collections Flashcards
What are the 4 rules for a valid .equals override?
- Reflexive: For any reference value x, x.equals(x) always returns true.
- Symmetric: For any reference values x and y. If x.equals(y) returns true, so should y.equals(x);
- Consistent: For any refernce values x and y. Multiple invocations of x.equals(y) should return the same value.
- For any non-null reference value x, x.equals(null) should return false.
True or False:
If x.equals(y) returns true, x.hashCode == y.hashCode may return false.
False, if x.equals(y) returns true, then both objects should generate the same hashcode.
Describe the steps for Hashing retrieval (f.e. HashMap).
- Find the right “bucket” using the hasCode() method of the object.
- Compare all the items in the bucket with the object using the equals method.
True or False:
If x.hashCode == y.hashCode is true, x.equals(y) is always true.
False! x.equals(y) may return false. HashCode only describes in which bucket the object should be stored (f.e. HashMap). However if x.equals(y) indeed returns true, then the hashCode methods should return the same value.
What is wrong with the following hashCode override?
public int hashCode() { return 999; }
It’s a valid override because it doesn’t break the contract. However will be very inefficient when used in Hash retrieval collections.
What are the 3 contract rules for the hashCode method?
- Whenever the method is invoked it should always consitently return the same value for a given object.
- Whenever x.equals(y), both x and y should generate the same hashCode.
- It is NOT required to generate unique hashCode values for two objects that are considered equal (using the equals method) but is recommended because of performance improvements.
What happens if you use a transient instance variable in determining the hashCode?
If you serialize and deserialize the object. The object may suddenly produce a different hashCode because the value is lost in the serialization proces. It will get it’s default value.
What are the basic flavors for Collections?
Lists, Sets, Maps and Queues
What are the sub-flavors for Collections?
Sorted, Unsorted, Ordered, Unordered
What is an ordered collection?
It means you can iterate through a collections in a specific not-random order. (f.e. ArrayList uses the index as the order).
What is an sorted collection?
It means the order in the collection is determined by some rule or rules, known as the sort order.
Describe the three List implementations, and whether they are ordered, unorderd, sorted and/or unsorted.
ArrayList, ordered (by index) but unsorted
Vector, ordered (by index) but unsorted (thread-safe, synchronized). Old java version.
LinkedList, ordered (by index) also implements the queue interface. Elements are linked.
Describe the three Set implementations and whether they are ordered, unorderd, sorted and/or unsorted.
HashSet, unsorted and unordered. Uses hashCodes for placing in the collection.
LinkedHashSet, unsorted but ordered. Use this if you care about the iteration order (order of insertion)
TreeSet, sorted. Using natural order (ascending) or custom defined order using Comparable or Comparator.
Describe the four Map implementations and whether they are ordered, unorderd, sorted and/or unsorted.
HashMap, unsorted and unorderd. Uses hashCodes for placing in the collection. Can contain a single NULL key and many NULL values.
Hashtable, unsorted and unorderd. Old java version. Cant’t contain NULL values as key or value.
LinkedHashMap, unsorted but ordered. Keeps insertion order.
TreeMap, sorted. Keeps natural order or custom order using the Comparable ar Comparator interfaces.
Describe the Queue implementations and whether they are ordered, unorderd, sorted and/or unsorted.
LinkedList, ordered (by index) also implements the queue interface. Elements are linked.
PriorityQueue, sorted by natural order or custom order if defined with a Comparable or Comparator implementation.