Collections Flashcards Preview

JAVA OCA7 > Collections > Flashcards

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

boolean equals (Object obj)

Decides whether two objects are meaningfully
equivalent

2

void finalize()

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

3

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

4

final void notify()

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

5

final void notifyAll()

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

6

final void wait()

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

7

String toString()

Returns a "text representation" of the object

8

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

The object's toString() method is called

9

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

HardToRead@a47e0
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.

10

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.

11

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.

12

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

No. equals method is public in Object class.

13

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.

14

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.

15

What is being reflexive for equals contract?

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

16

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.

17

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.

18

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.

19

Why is hashcode used for?

To increase the performance of large collections of data.

20

How does hashing retrieval work?

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

21

How does hashcode work for two equal objects?

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

22

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.

23

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.

24

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.

25

What are the core interfaces in Collections API?

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

26

What are the concrete implementation classes for Maps?

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

27

What are the concrete implementation classes for Sets?

HashSet, LinkedHashSet, TreeSet

28

What are the concrete implementation classes for Lists?

ArrayList, Vector, LinkedList

29

What are the concrete implementation classes for Queues?

PriorityQueue

30

What are the concrete implementation classes for utilities?

Collections, Arrays