Return to Coding in Java with artEoz – exercises

HashMap class

  1. Make sure you have the geometrie resource. If necessary, see the Resources import webpage.
  2. A table is a function that assigns a value to a key; a key can only have one value. The HashMap <K, V> class is a special implementation table (K is the key type and V value type). The search function is performed in an almost constant time, through the use of a so-called hash function, which converts the key to an integer.
  3. The statement below creates an empty hashmap:
  1. How many fields represent an HashMap? key_48 What do their names mean? key_48
  2. Add an entry to the table:
  1. Add a second key-value pair, then a third one. What happened? key_48 Entry instances are not adjacently stored in the table. Are you able to find the way used for the allocation of cells? key_48
  2. What happens if you added a new key-value pair with a key that already exists? key_48
  3. It is also possible to delete a key-value pair, using the remove function. Write the statement that removes a key and display the evolution of the memory representation. key_48It’s time to choose the “Unreferenced objects” option. What do you see? key_48
  4. The statement below creates a new empty hashmap:
  1. Add the baba key. What happens to the table of Entry instances? What is one Entry instance? key_48
  2. What happens if you add the new xxx key? key_48
  3. Write a call to the get function. key_48How is performed the get function? key_48 at can we conclude about the effectiveness of the get function? key_48
  4. Remove one by one all the points stored in the table (remove function).
    Is the table reorganized ? key_48
  5. Enter the instructions below. The goal is to create a hash table table1 containing 2 points, then to create table2 using the copy constructor of HashMap and to create table3 using the clone function. Look at the different memory implementations of these tables. Count the number of instances of the points then remove one point of table1.
    What do you notice? key_48
  1. The instructions sequence below is similar to the previous one, except that the points are accessible from variables. What do you notice? key_48