• 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

Collection question

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q220 Which of the following statements are true?
(1) The keys of HashSet are not ordered
(2) The keys of LinkedHashSet are ordered
(3) The keys of LinkedHashSet are ordered but not sorted
(4) The keys of LinkedHashMap are sorted
Ans is 1 2 3
But all the above statements are false.Coz there are no key in set & map...This was question from the link http://www21.brinkster.com/infoway02/asp/ShowAllQns4.asp?start=201&end=250
Please can anybody explain?

Veena
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LinkedHashMap has the keys. But it keeps keys in insertion order or last access order. No sorting is provided. Therefore, I will say none of the choices given are valid.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barkat Mardhani:
LinkedHashMap has the keys. But it keeps keys in insertion order or last access order.

Which is what is meant by "ordered" vs. "sorted". So 2&3 are true.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Javadoc for HashSet explicitly says that it's "backed by a... HashMap" meaning that the data is stored as the keys in a HashMap (not the values; it's only the keys that can really be "looked up" in a map.) If you look at the HashSet source, you'll see that the given data are used as the keys in a HashMap, and a singleton instance of Object is used as the data for all keys!
Given this, I think it's reasonable to say that 1) is true as well. I think the given answer is perfectly correct.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
posted by Paul:

quote:
--------------------------------------------------------------------------------
Originally posted by Barkat Mardhani:
LinkedHashMap has the keys. But it keeps keys in insertion order or last access order.
--------------------------------------------------------------------------------
Which is what is meant by "ordered" vs. "sorted". So 2&3 are true.


But Sets do not have keys. Only Maps do. So the basic premise is wrong for first three options. With regards to last option, LinkedHashMap do have keys but not sorted. So that is also invalid. At least that is how I look at it.
Thanks
Barkat
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sets do have keys. When you instantiate a HashSet, this is what happens.
public HashSet() {
map = new HashMap();
}
Furthermore, when you instantiate a LinkedHashSet, the constructor invokes its superclass, ie a HashSet:
public LinkedHashSet(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor, true);
}
So, the answer of 1 2 3 is correct.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused now. I thought concept of keys is associated with Maps only. That is concept of key/value pairs. However, Sets contain set of unique values. No keys involved.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I am confused too. Which kind of key the question is talking about?
1) If it is talking about the key need to be passed in by user as key/value pair, then no key for any set at all. Here, user means people using the set collection.
2) If the key means something inside the infrastructure of set collection, which is kind of map. Of course, HashSet is using map as base structure and do have keys too.
However, what is the point here, I mean in this test(SCJP). Is �using key or not� a basic difference between set and map? If the answer is �yes�, why we should worry about keys of HashSet? Will the test objective cover the key of Set infrastructure?
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it unlikely that you will get this kind of question in the exam. However, it is certainly useful to know that the reason why Set elements are unique is because they are stored as keys in a HashMap.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The implementation details of a Set should be hidden from its interface. I think wording of question surely is misleading. Regardless of inclusion of implementation details of a Set in the exam, term 'keys' in the question is referring to keys in key/value pairs.
Barkat
[ September 29, 2003: Message edited by: Barkat Mardhani ]
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Barkat partially - but it has to be noted that the class in question is HashSet, not Set. The things is, HashSet mentions parenthetically in its API doc that it is backed by a HashMap:

This class implements the Set interface, backed by a hash table (actually a HashMap instance).

My question is: does this constitute a contract? Can/should we assume that from now til eternity HashSet will be backed by a HashMap? There is nothing in its public interface to prevent reimplementation by some other mechanism.
[ September 29, 2003: Message edited by: Steve Lovelace ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the question author simply screwed up. Yes, we can observe that HashSet is backed by HashMap, etc, but "backed by" is rather poorly defined anyway. Nothing in the APIs for Set, HashSet or LinkedHashSet says anything about "keys", and using that terminology is unnecessarily confusing. Just pretend the author said "entries" instead of "keys" and the whole thing works much better. Real test questions are better worded than this; this type of mistake isn't worth worrying about, IMO.
reply
    Bookmark Topic Watch Topic
  • New Topic