• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Sybex 1Z0-829 page 478 chapter 9

 
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
This is on page 478, subtitled "Working with Set Methods":

Like a List, you can create an immutable Set in one line or make a copy of an existing one.

Those are the only extra methods you need to know for the Set interface for the exam!


Nowhere in the text it mentions that the Set.of() method as given will throw an exception. Other code examples mark lines with errors.
"Duplicate elements passed to a static factory method result in IllegalArgumentException."
 
Bartender
Posts: 3958
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't comment on the book as I don't have it handy, but I confirm that duplicated values for the factory method are forbidden.

And there are some more restrictions (e.g. no null elements allowed)

Let me quote the JavaDoc:


Unmodifiable Sets

The Set.of and Set.copyOf static factory methods provide a convenient way to create unmodifiable sets. The Set instances created by these methods have the following characteristics:

  • They are unmodifiable. Elements cannot be added or removed. Calling any mutator method on the Set will always cause UnsupportedOperationException to be thrown. However, if the contained elements are themselves mutable, this may cause the Set to behave inconsistently or its contents to appear to change.
  • They disallow null elements. Attempts to create them with null elements result in NullPointerException.
  • They are serializable if all elements are serializable.
  • They reject duplicate elements at creation time. Duplicate elements passed to a static factory method result in IllegalArgumentException.
  • The iteration order of set elements is unspecified and is subject to change.
  • They are value-based. Programmers should treat instances that are equal as interchangeable and should not use them for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail. Callers should make no assumptions about the identity of the returned instances. Factories are free to create new instances or reuse existing ones.
  • They are serialized as specified on the Serialized Form page.



  •  
    Enthuware Software Support
    Posts: 4907
    60
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sets don't allow duplicates irrespective of how you create them or which implementation you use.
     
    Marshal
    Posts: 80765
    488
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Anilprem wrote:Sets don't allow duplicates irrespective of how you create them or which implementation you use.

    Of course; that is part of the definition of a Set. But if I wrotethe code would run to normal completion, with line 4 returning false rather than throwing an exception. The semantics of add() and the of() factory method are different.
     
    Ira Go
    Ranch Hand
    Posts: 79
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    My point is that the example given in the book is deceiving. Instead, it could be used to show how of() and copyOf() behave.
     
    author & internet detective
    Posts: 42151
    937
    Eclipse IDE VI Editor Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ira Go wrote:My point is that the example given in the book is deceiving. Instead, it could be used to show how of() and copyOf() behave.


    It's not deceiving; it's wrong . Added to the errata and credited you.
     
    Ira Go
    Ranch Hand
    Posts: 79
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you, Jeanne, that's cool.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic