Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

SET function

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
See the example below.

The author says, "When you are creating your own types, be aware that a set needs a way to maintain a storage order, which mean you must implement the Comparable interface and define the ComparableTo method"

The code is appended below.

Questions,
1) What author means here ?
2) When and How the comparaTo method is invoked and used ?
3) In the example, why we need hashCode() and equals() methods?
how it is being used ?







thanks
siva
[ June 25, 2004: Message edited by: Siva kandasamy ]
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Siva,

It appears that you are reading a book that covers the use of the Java Collections Framework. Since the author advocates the implementation of the Comparable interface, it appears that the author is discussing the use of Collection implementations that maintain object instances in a sorted order. For more information on the Java Collections Framework, please see the Javaranch News Letters. A series of four articles on the Collections Framework appear in the June 2002 through September 2002 issues.
 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Don,

Look at the code below, why method
"public int compareTo(Object o)" never called to compare ?

My understanding is, compareTo will be invoked for the each object I add to the set "a".

Thanks you very much.

 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A HashSet is backed by a HashMap. The objects stored in a HashSet are used as the keys for the HashMap. The values associated with each key in the backing HashMap are just dummy values. Actually, all of the keys share the same dummy value. All of the keys stored in a HashMap must be unique, so the keys of a HashMap are always a set of unique objects.

When a new key/value pair is stored in the backing HashMap, the HashMap.put method will use the equals method of the key to see if the key has already been used. If the key has been used, then the new key/value pair replaces the old key/value pair. The HashMap.put method does not invoke the compareTo method on the key object.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Intermediate forum...
 
reply
    Bookmark Topic Watch Topic
  • New Topic