You application requires to store name value pairs such that the order of entries returned while iterating through the structure is deterministic. In other words, if you iterate over the same structure twice, the order of elements returned in both the iterations will be the same. Which of the following classes would you use?
Select 2 correct options a HashMap b LinkedHashSet c HashTable d LinkedHashMap e TreeMap It always returns the entires in sorted order. Ans is d and e... even hashmap and hashtable return in same way as same hashing algo will be used all the time... pls help
Basically the HashMap and HashSet use the hashcode method to determine the placement of the object on the hash, whereas the others use Comparators to determine their order for sorted Collections. It is not to say that you can't change the hash method to get the sort you actually wanted, but it is using the method for the wrong purpose. With a hashcode method, you want the most evenly dispursed code to come out so that the hash tree isn't lop-sided.
So say you have objects in your lawn and you want to spread them around evenly, so that one part of the lawn isn't over loaded with lots of objects and therefore kills all your grass in that area. Plus, it will be easy to grab one object, if you don't have to go hunting through a huge pile in one area of your lawn, because that is where you piled everything upon.
Now a Comparator and sorting is different, you just want to have it is a particular order all the time, there isn't a care for putting everything in one area. If you are sorting by alphabet, and there happens to be a lot of "A" words, then you want them in correct order, and don't care that there are a lot of "A"s.
Nothing in the original question requires the results to be sorted - they just have to be ordered. Sorting implies that there's either a Comparable or a Comparator involved, and that the order of objects is determined by intrinsic properties of those objects. Being ordered just meanst that the position is significant in some way rather than random (or seemingly random). E.g. an ArrayList is ordered, but not sorted. (Unless you use Collections.sort() on it. LinkedHashSet and LinkedHashMap don't use Comparators, and they're not sorted -- they don't implement SortedSet or SortedMap. They do maintain insertion order. (Or last access order if you prefer that instead.)
Actually the question is still ambiguous:
In other words, if you iterate over the same structure twice, the order of elements returned in both the iterations will be the same.
I would argue that HashMap and Hashtable (there's no such class as HashTable) do fulfil this condition - provided you don't add or remove anything between iterations. That's probably not what the question author meant, but it does show a problem with the question itself. The previous wording about being "deterministic" could be interpreted to exclude HashMap and Hashtable, on the grounds that some classes like Object do not have deterministic hashCode() methods. But "in other words" falsely leads us to believe these two descriptions mean the same thing, when in fact they don't. Still, given that we are told to select 2 options, there's no question that HashMap and Hashtable are the two Maps which least seem to fulfil the requirements - even allowing for some ambiguity.
"I'm not back." - Bill Harding, Twister
Space pants. Tiny ad:
a bit of art, as a gift, that will fit in a stocking