Advantages and Disadvantages of using maps in Java?
What are some advantages/disadvantages of using maps such as hashmaps and treemaps in Java?
Also, as a side question, what are some examples of when you'd use a stack and a queue?
Thanks in advance! :)
- OrdalcaLv 59 years agoFavorite Answer
Maps use objects with links between them in order to sort data. This use means that the technique uses more memory than in an array for the same amount of data. The waste is minimized by the automatic garbage collection in Java. The advantage to using maps far outweighs the cost. By using a map, the data is sorted in such a fashion that searching for it is much faster.
The binary search tree takes O(log n) and a hash tables takes O(1), compared to an array's O(n).
Tree mapping and Hash maps are simply ways of displaying the data sorted above while also conveying the sorted information, making searching for a random piece of data easier.
Stacks are used when you want the last piece of data you put in to be the first one retrieved. This is used in things such as compilers and state machines. The most basic is the call stack for a program. This recalls the order of methods, allowing the return function to work, as well as allowing for modularity in a program by separating bits of code into methods.
Queues are used when you want the first piece of data you put in to be the first one retrieved. This is used in cases like the simulation of production lines, the sending of information packets in the internet, and in other situations as a buffer, storing data in order of occurrence to use in the future.
- Anonymous6 years ago
Check out the detail articles on benefits of using Hashmaps
Hashing is an algorithm which maps data to some representative number. It is implemented in Java Collection to efficiently store and search data from it. HashMap also allows null value as key.