Collections Flashcards Preview

JAVA OCA7 > Collections > Flashcards

Flashcards in Collections Deck (73)
Loading flashcards...

boolean equals (Object obj)

Decides whether two objects are meaningfully


void finalize()

Called by the garbage collector when the garbage
collector sees that the object cannot be referenced


int hashCode()

Returns a hashcode int value for an object
so that the object can be used in Collection
classes that use hashing, including Hashtable,
HashMap, and HashSet


final void notify()

Wakes up a thread that is waiting for this object's


final void notifyAll()

Wakes up all threads that are waiting for this
object's lock


final void wait()

Causes the current thread to wait until another
thread calls notify() or notifyAll() on
this object


String toString()

Returns a "text representation" of the object


What happens when you pass an
object reference to the System.out.println() method?

The object's toString() method is called


What does System.out.println() print when you do not override toString()?

It gives you the class name (at least that’s meaningful)
followed by the @ symbol, followed by the unsigned hexadecimal representation of the object’s hashcode.


How does == works by default?

Evaluates to true only when both references refer to the same object because == simply looks at the bits in the variable, and they're either identical or they're not.


What happens when you use collections if you do not override equals?

You can't use an object as a hashtable key. Assume you put a Car object to hash with associated Person. Later you say give me the Person with Car X.But now you're in trouble unless you still have a reference to the exact object you used as the key when you added it to the Collection. In other words, you can't make an identical Car object and use it for the search.


Is this override valid;
class Foo { boolean equals(Object o) { } }

No. equals method is public in Object class.


Is this override valid;
class Boo { public boolean equals(Boo b) { } }

When we look carefully we can see that it's an overloading not overriding.


What are the properties of equals method according to contract?

It is reflexive, symmetric, transitive, consistent. And for any non-null reference value x.equals(null) should return false.


What is being reflexive for equals contract?

For any reference value x, x.equals(x) should return true


What is being symmetric for equals contract?

For any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.


What is being transitive for equals contract?

For any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) must
return true.


What is being consistent for equals contract?

For any reference values x and y, multiple invocations of
x.equals(y) consistently return true or consistently return false, provided no information used in equals() comparisons on the object is modified.


Why is hashcode used for?

To increase the performance of large collections of data.


How does hashing retrieval work?

1- Find right bucket using hashCode
2-Search the bucket for the right element using equals


How does hashcode work for two equal objects?

If two objects are equal their hashcodes must be equal as well.


Is it legal to return same hashcode for all the objects?

Yes it is legal. It does not violate the contract but it is not efficient.


What is the contract for hashcode?

- If two objects are equal according to equals they must return same hashcode.
- During execution of a Java app. hashcode must return same value everytime, provided no info used in equals.
- Two unequal objects can return same hashcode.


What is the problem with using transient in equals or hashcode implementation?

When an object is seralized, transient values are nor included. And when they are deserialized back, these values get default values. So the two instances are not same any more. Keep variables non-transient or, if they must be marked transient, don't use them to determine hashcodes or equality.


What are the core interfaces in Collections API?

Collection, List, Queue, Set, Map, NavigableSet, SortedSet, SortedMap, NavigableMap


What are the concrete implementation classes for Maps?

HashMap, Hashtable, TreeMap, LinkedHashMap - but they are not extended from Collection!


What are the concrete implementation classes for Sets?

HashSet, LinkedHashSet, TreeSet


What are the concrete implementation classes for Lists?

ArrayList, Vector, LinkedList


What are the concrete implementation classes for Queues?



What are the concrete implementation classes for utilities?

Collections, Arrays