6. Generics and Collections Flashcards
Which of the collection classes are thread safe?
Vector and hashtable are thread safe
What has to be unique in a map?
All keys in a map must be unique. The values can be the same
What does the values() return?
The values() method returns a collection
What does Arrays,binarySearch() return?
This method returns the index of the search key, if it is in the list
With map supports adding null key as wel as null values?
Hashmap
equals() and hashCode() methods
Whenever you need to sort or search through a collection of objects, the equals() and hashCode() methods are essential. When you really need to know if two references are identical, use ==. But when you need to know if the objects themselves are equal, use the equals() method.
What happens when a object does not past the instanceOf test?
First, when overriding the equals() method, be sure that the object being tested is of the correct type! It comes in as type Object, so do a instanceof test. Remember, if the object doesn’t pass the instanceof test, you will get a runtime ClassCastException.
rules for overriding equals()
- Reflexive: for any reference value x, x.equals(x) should return true
- Symmetric: x.equals(y) should return true if and only if y.equals(x) return true
- Transitive: if x.equals(y) return true and y.equals(z) return true, then x.equals(z) must return
true - Consistent: multiple invocations of x.equals(y) consistenly return true or consistently return
false - for any non-null reference value x, x.equals(null) should return false.
Hashcode() contract
- Whenever it is invoked on the same object more than once during an execution of a java
application, the hashCode() method must consistently return the same integer, provided that
no information used in equals comparisons on the object is modified - If two objects are equal according to the equals(Object) method, then calling hashCode()
method on each of the two objects must produce the same integer result - It is not required that if two objects are unequal according ti the equals(Object) method,
then callin the hashCode() method on each of the two objects must produce distinct integer
result
Wich 14 concrete Collection implementation classes you need to know for the exam?
HashMap HashSet ArrayList PriorityQueue Collections
Hashtable LinkedHashSet Vector ArrayDeque Arrays
TreeMap TreeSet LinkedList
LinkedHashMap
Which four basic flavours do Collections come in?
- Lists: Lists of things (classes that implement List)
- Sets: Unique things (classes that implement Set)
- Maps: Things with a unique ID (classes that implement Map)
- Queues: Things arranged in the order in which they are to be processed.
which four sub flavours do Collections come in?
Sorted, Unsorted, Ordered,
Unordered.
What does Ordered mean?
When a collection is ordered, it means you can iterate through the collection in a specific
(not random) order. A Hashtable is not ordered. ArrayList, keeps the order established by the
elements index position. LinkedHashSet keeps the order established by insertion, so the last element inserted is the last element in the LinkedHashSet.
What does Sorted mean?
sorted means that the order in the collection is determined according to some rule or rules,
known as the “sort order”. Sorting is done based on properties of the objects themselves
What is the List interface?
A List cares about the index. The one thing that List has that nonlists don’t is a set of methods related to the index (get(int index), indexOf(Object o), add(int index, Object o)). The three list
implementations:
- ArrayList: think of this as a growable array. It is an ordered collection (by index) but not
sorted. ArrayList implements the RandomAccess interface (marker interface that says: this
list support fast random access). - Vector: A Vector is basically the same as an ArrayList, but Vector methods are synchronized
for thread safety. Of the classes discussed here, only Vector and ArrayList implement
RandomAccess - LinkedList: ordered by index postion, except that the elements are doubly linked to one
another. This linkage gives you new methods, for adding and removing from the beginning or
end. LinkedList may iterate more slowly than an ArrayList, but its a good choice when you
need fast insertion and deletion.
What is the Set interface?
A set cares about uniqueness, it doesn’t allow duplicates. The equals() method determines whether two objects are indentical (in which case, only one can be in the set). The three set implementations:
- HashSet: a HashSet is an unsorted, unordered Set. It uses the hashcode of the object being
inserted, so the more efficient your hashCode() implementation, the better access
performance you will get. Use this class when you want a collection with no duplicates and
you don’t care about order when you iterate through it - LinkedHashSet: A LinkedHashSet is an ordered version of HashSet that maintains a doubly
linked List across all elements. A LinkedHashSet lets you iterate through elements in the
order in which they were inserted. - TreeSet: The treeset is one of two sorted collection (otherone: treemap). It guarantees that
the element will be in ascending order, according to natural order. Optionally you can
construct a TreeSet with a constructor that lets you give the collection your own rules for
what the order should be by using a Comparator.
What do you need to when using HashSet or LinkedHashSet?
When using HashSet or LinkedHashSet, the objects you add to them must override
hashCode(). If they don’t override hashCode(), the default hashcode method will allow multiple
objects that you might consider “meaningfully equal” to be added to your “no duplicates allowed” set.
What is the Map interface?
A map cares about unique identifiers. You map a unique key (the ID) to a specific value, where both the key and the value are objects. The map implementations let you do things like search for a value based on the key, ask for a collection of just the values. Maps rely on the equals() method to determine whether two keys are the same or different.
- HashMap: The hashMap gives you an unsorted, unordered Map. When you need a Map and
you don’t care about the order when you iterate through it, then HashMap is the way to go.
Where the keys land in the Map is based on the key’s hashcode, so like HashSet. HashMap
allows one null key and multiple null values in a collection. - Hashtable: Hashtable is the synchronized counterpart to HashMap. We just mean that the
key methods of the class are synchronized. And a Hashtable doesn’t let you have anything
that’s null - LinkedHashMap: Like its Set (LinkedHashSet) counterpart. it will be somewhat slower than
hashMap for adding and removing elements, you can expect faster iteration with a
LinkedHashMap. - TreeMap: TreeMap is a sorted Map – sorted by the natural order of the elements. Via a
Comparator you can define a custom sort order.