Q&A

Q. Why use immutable symbol-table keys?

A. If we changed a key while it was in the hash table or BST, it could invalidate the data structure’s invariants.

Q. Why is the val instance variable in the nested Node class in HashST declared to be of type Object instead of Value?

A. Good question. Unfortunately, as we saw in the Q&A at the end of SECTION 3.1, Java does not permit the creation of arrays of generics. One consequence of this restriction is that we need a cast in the get() method, which generates a compile-time warning (even though the cast is guaranteed to succeed at run time). Note that we can declare the val instance variable in the nested Node class in BST to be of type Value because it does not use arrays.

Q. Why not use the ...

Get Introduction to Programming in Java: An Interdisciplinary Approach, second edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.