Class ItemHashMap

java.lang.Object
java.util.AbstractMap<IdHashKey, Item>
java.util.HashMap<IdHashKey, Item>
org.troy.capstone.data_structures.item_table.ItemHashMap
All Implemented Interfaces:
Serializable, Cloneable, Map<IdHashKey, Item>, ItemRepo

public class ItemHashMap extends HashMap<IdHashKey, Item> implements ItemRepo
Custom HashMap implementation for storing Item instances with their IDs as keys, using a custom universal hash function (IdHashKey) to optimize bucket distribution for the specific set of item IDs in our dataset.
See Also:
  • Field Details

    • MAX_LOAD_FACTOR

      private static final float MAX_LOAD_FACTOR
      Maximum load factor for the ItemHashMap.
      See Also:
    • TABLE_SIZE

      private static final int TABLE_SIZE
      Table size for the ItemHashMap, chosen to be a power of 2 for efficient modulo operations, and large enough to maintain a load factor of 0.75 for our expected number of items (961).
      See Also:
  • Constructor Details

    • ItemHashMap

      private ItemHashMap(int data_size)
      Make initial capacity so that we have just over a 0.75 load factor.
      Parameters:
      data_size - The number of Item instances that will be added to the map, used to get a 0.75 load factor.
      Preconditions:
      data_size is a positive integer.
  • Method Details

    • fromTable

      public static ItemHashMap fromTable(Table table)
      Creates an ItemHashMap from a Table. The map is initialized with the optimal hash parameters for the current item IDs.
      Parameters:
      table - A tablesaw Table containing the item data, with each row representing an item.
      Returns:
      itemMap An ItemHashMap containing all items from the table, with hash parameters optimized for the item IDs in the table.
      Preconditions:
      table is not null and contains the expected columns for creating Item instances (ID, Name, etc.), and contains no duplicate IDs.
    • addItem

      private void addItem(Row itemRow)
      Adds an item to the hashmap given a tablesaw Row.
      Parameters:
      itemRow - A Row from a tablesaw Table containing item info.
      Preconditions:
      itemRow is not null and contains the expected columns for creating an Item instance (ID, Name, etc.).
    • addAllItems

      private void addAllItems(Table table)
      Adds all rows from a tablesaw Table to the hashmap as Items.
      Parameters:
      table - A tablesaw Table with each row being an item to add to the map.
      Preconditions:
      table is not null and contains the expected columns for creating Item instances (ID, Name, etc.), and contains no duplicate IDs.
    • getItem

      public Optional<Item> getItem(String itemId)
      Retrieves an item from the ItemHashMap given its ID. Prints a message if the item is not found in the map.
      Specified by:
      getItem in interface ItemRepo
      Parameters:
      itemId - The ID of the item to retrieve.
      Returns:
      An Optional containing the Item if found, or empty if not found.
      Preconditions:
      itemId is not null and corresponds to a valid item ID in the map.
    • getFreshBucketSizeCount

      public int[] getFreshBucketSizeCount(List<String> itemIds, boolean useCustomHash)
      Calculate bucket distribution with current I and J values (fresh hash calculation).
      Parameters:
      itemIds - A list of item IDs to calculate the bucket distribution for.
      useCustomHash - Whether to use the custom IdHashKey hash function or the built-in String hashCode.
      Returns:
      bucketSizeCounts An array where the value at index N is the number of buckets that have N items in them, according to the specified hash function.
      Preconditions:
      itemIds is not null and contains valid item IDs.
    • getKeysAsList

      private List<IdHashKey> getKeysAsList()
      Retrieves the keys of the ItemHashMap as a list of IdHashKey objects.
      Returns:
      A list of IdHashKey objects representing the keys in the ItemHashMap.
    • getItemIdsAsList

      public List<String> getItemIdsAsList()
      Retrieves the item IDs of the ItemHashMap as a list of strings.
      Returns:
      A list of strings representing the item IDs in the ItemHashMap.
    • getItemsAsList

      public List<Item> getItemsAsList()
      Retrieves the items of the ItemHashMap as a list of Item objects.
      Specified by:
      getItemsAsList in interface ItemRepo
      Returns:
      A list of Item objects representing the values in the ItemHashMap.
    • printBucketSizeCountsCustomVsBuiltIn

      private void printBucketSizeCountsCustomVsBuiltIn()
      Prints a table comparing the distribution of bucket sizes (number of buckets with 0 items, 1 item, 2 items, etc.) for the custom hash function vs Java's built in String hashCode, using the same item IDs. This allows us to see how well our custom universal hash function is performing in terms of distributing items across buckets compared to the built-in hash function.
      Preconditions:
      findBestHashParameters() has already been called to optimize I and J for the current item IDs, the internal state of the ItemHashMap is not modified between the two distribution calculations (i.e. no items are added or removed), the same item IDs are used for both calculations, and the map must be filled.
    • copy

      Creates a deep copy of the ItemHashMap.
      Returns:
      A new ItemHashMap containing the same entries as the original.
      Throws:
      CloneNotSupportedException - if any of the keys or values in the map do not support cloning.
    • getSize

      public int getSize()
      Retrieves the number of items in the repository.
      Specified by:
      getSize in interface ItemRepo
      Returns:
      The number of items in the repository.