• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Which collection to use?

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A and C are worng because 1) They are not guaranteed to return the same order when iterating. 2) They are not sorted sets/maps.

Mark
 
Aruna Agrawal
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Spritzler:
A and C are worng because 1) They are not guaranteed to return the same order when iterating. 2) They are not sorted sets/maps.

Mark


why they are not guaranteed to return.. i guess because hashcode is a native method and uses raw memory adderss to compute hashcode for object...

so i close this topic.. assuming what i said is right... thanks Mark..
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Hope that helps clarify things.

Mark
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic