Java Collections Framework Implementations 1. Explain why it is important that e
ID: 3576011 • Letter: J
Question
Java Collections Framework Implementations 1. Explain why it is important that elements that are stored in a Set or Map aren't modified in a way that affects the outcome of the equals() method. 2. Explain why you would choose a LinkedHashSet (or LinkedHashMap) over a Hashset or HashMap, respectively) 3. Describe the running time of the methods get (i) and set (i,x) for an ArrayList versus a LinkedList 4. Describe the running time of the method add (i,x) for an ArrayList versus a LinkedList 5. Explain why it is possible to quickly make a lot of modifications in the interior (not near either end) of a LinkedList but this is not possible in an ArrayList. 6. For each of the following methods, decide if it is fast or slow when (a) l is an ArrayList and (b) when l is a LinkedList.Explanation / Answer
It is important that elements that are stored in a Set or Map arent't modified in a way that affects the outcome of the equals() method.
Two very common collection implementations are HashSet and HashMap which uses a hash table data structure. A hash table is a representation for mapping i.e. it is an abstract data type that maps keys to values. Keys don't have to be ordered or have a particular property except for offering "equals" or "hashCode".
If two equal objects have different hash codes, they might be places in different slots. So if you attempt to lookup a value using a key equal to the one with which it was inserted, the lookup may fail.
Object’s default hashCode() implementation is consistent with its default equals():
public class Object{
.
.
public boolean equals(Object what)
{return this == what;}
public int hashCode()
{return /* the memory address of this */;}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.