Examen de rattrapage NFA035 Java
Examen de rattrapage NFA035 Java
The HashMap implementation employs dynamic reallocation as a strategy to handle potential capacity limitations of arrays. The realloc method is used when adding an element would exceed the current capacity of the array. This method extends the array's capacity by a predefined constant value, which allows the list to grow dynamically as more elements are added. Additionally, the check for available space (via the condition if(last == elem.length-1)) is integral to deciding when to perform a reallocation to prevent array overflow .
In this context, Java interfaces and abstract classes are utilized to create models similar to the Java Collection Framework by defining a set of methods in the MList interface that dictate the expected behavior of the lists, such as size, isEmpty, contains, and others. The abstract class AbsList provides a partial implementation of these methods, allowing common functionality to be shared while still being able to override or expand upon in concrete subclasses like UniqueList and RegularList. This approach promotes code reuse, modularity, and the separation of interface and implementation, aligning with the design principles of the Java Collection Framework .
Advantages include simplicity and direct access through array indices, which can make certain operations like get and indexOf relatively fast compared to more complex structures. Arrays also have a lower memory overhead compared to linked structures. However, disadvantages include poor performance in dynamic size adjustments, as reallocations are costly in terms of time complexity. Arrays have limited capacity that needs to be managed manually, requiring reallocation logic to handle increases, which can slow down applications significantly when large re-sizes occur frequently .
Using arrays to implement functionalities like add and realloc in a generic HashMap has several implications. For add, the process involves checking if the array is full, and if so, reallocating or extending the array, which can be costly in terms of performance due to the need to copy the entire array to a new, larger array. This realloc function extends the capacity of the array by a constant value, providing dynamic growth. Additionally, using arrays implies a predefined type, so when reallocating, type casting is necessary, which could introduce potential issues or require additional handling for type safety .
The HashMap's get method utilizes the lists by first checking if the keys list contains the specified key. If the key exists, it retrieves the index of this key using the indexOf method and then uses this index to fetch the corresponding value from the values list. This approach reveals that key-value relationship maintenance relies on synchronized indexing between the keys and values lists, meaning that every key's associated value is stored at the same index position in the values list, ensuring accurate retrieval .
The presented HashMap implementation shares some functional similarities with Java's built-in HashMap, such as storing key-value pairs and using methods like put and get. However, it diverges in several ways due to its simplistic linear data structure basis, like relying on two separate lists using array-backed storage. Unlike Java's HashMap, which uses a more efficient hash table approach for constant-time performance, this implementation faces potential inefficiencies due to its dependence on list traversal for operations like contains and indexOf, leading to linear-time complexity in worst-case scenarios. Additionally, this approach does not support rehashing or collision resolution typically handled in Java HashMaps, which suggest potential limitations in scalability and efficiency compared to optimized Java implementations .
The implementation utilizes two separate lists, one for keys and the other for values, both backed by arrays. The keys list is of type UniqueList, which ensures uniqueness by only adding elements if they are not already contained in the list. This prevents duplicate keys. On the other hand, the values list is of type RegularList, which allows duplicates, meaning the values can be duplicated. The correspondence between keys and values is maintained by using matching indices in both lists .
The UniqueList and RegularList classes ensure proper functioning of the HashMap by managing the storage and organization of keys and values, respectively. UniqueList is responsible for ensuring that each key is unique, thus preventing duplicates, which is fundamental for a key-value pair structure like HashMap. Meanwhile, RegularList allows for the storage of values, including duplicates, providing the flexibility needed to associate multiple values with different keys. These specialized list classes support the HashMap's requirements by implementing necessary operations such as add, get, and contains, tailored to the rules of key uniqueness and value flexibility .
The 'realloc' method enhances the functionality of lists by allowing them to increase in capacity dynamically. This method is invoked when the list is full (i.e., when attempting to add an element to an already full array). By copying the elements to a new array with an increased length, the 'realloc' method resolves size constraints that would otherwise limit the addition of new elements. This ensures that the list can continue to operate efficiently without crashing due to overflow, supporting the dynamic nature required by a HashMap where entries are modified frequently .
The put method contributes to data integrity by first attempting to add the key to the UniqueList, which ensures that it is unique. If the key is successfully added, indicating it wasn't previously present, it then adds the corresponding value to the RegularList. This method only adds new entries if the key is not already in the list, thus maintaining a consistent one-to-one correspondence between each unique key and its value, ensuring that values are only associated with newly added keys .