Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Set interface in K&B SCJP book

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kathy and Bert SCJP BooK page 562

Set Interface
A Set cares about uniqueness—it doesn't allow duplicates. Your good friend the
equals() method determines whether two objects are identical (in which case only
one can be in the set). The three Set implementations are described in the following
sections.



My diffrent reading for the above:
for uniqueness HashSet and LinkedHashSet use the equals() method, but TreeSet uses the object compareTo() method inhertied from the Comparable interface to determine uniqueness, even if the object does not has an equals() or hashCode() methods. " This also applied to the Map Interface"
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the same book page 563

Optionally, you can
construct a TreeSet with a constructor that lets you give the collection your own
rules for what the order should be (rather than relying on the ordering defined by
the elements' class) by using a Comparable or Comparator. As of Java 6, TreeSet
implements NavigableSet.



Actually the natural order is determind by the programer who implemented the compareTo() method from the Comparable interface into the elements.
but if you wish a different ordering you can only use: Compartor as the TreeSet constructors shows TreeSet API:"This also applied to the Map Interface"

TreeSet()
Constructs a new, empty tree set, sorted according to the natural ordering of its elements.
TreeSet(Collection<? extends E> c)
Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements.
TreeSet(Comparator<? super E> comparator)
Constructs a new, empty tree set, sorted according to the specified comparator.
TreeSet(SortedSet<E> s)
Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.

 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the Comparable-Comparator thing I agree that it should only be Comparator. For Set thing, the contact of Set interface defines that equality is checked through equals method. Its another thing that TreeSet uses the compareTo method for it. But for the sake of clarity your statement is right as many people get confused over this...
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you Ankit, this is mentioned in Set Interface API

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.


But I think this is due to the lazyness of the documenting people in Sun or "I don't know whom", because its apperantly the above quote is copied litteraly from the API of the Set Interface before the NavigableSet

regards
[EDIT]ed for spelling mistakes, sorry[EDIT]
 
Something about .... going for a swim. With this tiny ad ...
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic