• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Sybex 1Z0-815 study guide (Boyarsky & Selikoff) Error, Chapter 6 page 383

 
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Page 383 "It turns out the keySet() and values() methods each return a Set."

I don't think this is correct. Map.KeySet() returns a Set, but Map.values() does not return a Set.

A set cannot contain duplicate values. A map can contain duplicate values (only the keys are unique). I wrote some code to check this:




output:
1
1

If we had a set, we should not see 1 printed twice. A set cannot contain duplicate values.

I also  tried this:



output:
false
true
 
Enthuware Software Support
Posts: 4428
41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right. It returns a Collection and not a Set. Mentioned in the JavaDoc as well.
You might want to check the errata for the book though as it might already be listed there.
 
Chilobe Mwanza
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Anilprem wrote:That's right. It returns a Collection and not a Set. Mentioned in the JavaDoc as well.
You might want to check the errata for the book though as it might already be listed there.



Yep, checked and its not listed.

 
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Thank you for the good details about the book in the th‍read title.
That is a very clever catch; entrySet() and keySet() both return Sets, because keys and therefore entry pairs must be unique, but values need not be unique, so that method returns a Collection whose cardinality is equal to the cardinality of the original Map, even if the values aren't unique.
 
Chilobe Mwanza
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Thank you for the good details about the book in the th‍read title.
That is a very clever catch; entrySet() and keySet() both return Sets, because keys and therefore entry pairs must be unique, but values need not be unique, so that method returns a Collection whose cardinality is equal to the cardinality of the original Map, even if the values aren't unique.



Thanks   I've been lurking for some time, so I'm glad I found something to post about!
 
You may have just won ten million dollars! Or, maybe a tiny ad.
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic