Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38558
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Is there a better way to group and sort objects? |
| Date | 2018-08-14 14:06 +0200 |
| Message-ID | <ftfuunF4g0lU1@mid.individual.net> (permalink) |
| References | (1 earlier) <pke94a$j46$1@news.albasani.net> <pkeib2$9pv$1@dont-email.me> <pkejoq$ksf$1@news.albasani.net> <ac2eee07-9f11-486f-9cc4-a46a07121d87@googlegroups.com> <toString-20180808154745@ram.dialup.fu-berlin.de> |
On 08.08.2018 16:50, Stefan Ram wrote:
> Eric Douglas <e.d.programmer@gmail.com> writes:
>> I'm using toString() to pass out all values that make the
>> address unique. This is my map key, so if my ship to source
>> contains an address that matches everything from a mail to
>> source I combine those to get one object that is flagged as
>> both ship to and mail to. To combine the key must be the
>> string, not the object as both objects would use unique
>> object pointers if you use them as a key.
>
> A class should be written so that its objects meaningfully
> represent an entity, and the standard methods like
> »compareTo« and »equals« should implement the natural
> ordering and equality properties of such an entity observing
> all the contracts from java.lang.Object and Comparable.
>
> These standard methods should not be tuned to "work" for a
> specific use case (Map) when this compromises their natural
> semantics or the standard semantics and contracts of the JDK
> (java.lang.Object, java.lang.Comparable).
>
> If such an entity then is to be used in an application where
> a specific order or merge behavior is required that is /not/
> implemented by the normal ordering and equality behavior of
> such an entity, one should not compromise the standard
> methods of this entity by modifying them for a specific use
> case, but instead use something like an adapter or some such.
>
> For example, an object might have a method »getKey« that
> returns an object that is intended to be used as a key for
> the object in certain maps. It is not obvious whether a
> string is the best type for such a key.
>
> »toString« should return a meaningful string representation
> of an object for a human seeing its value in error or log
> messages. It is not intended to provide a key to be used in maps.
I could not agree more. A few more thoughts:
It seems the class design can be improved, e.g. by separating out the
"8-10 fields that make up the address" in a class of its own. That
could have proper hashCode() and equals() implementations and maybe a
default order (once Eric catches up on the basics).
I also think the flags should be removed altogether (basically leading
to the same result as above) since the sort order is determined by the
user ("One user wants to see all mail to addresses first, another needs
to see ship to addresses first."), i.e. it is not part of the
(presumably persistent) data but rather of the _use_ of it (i.e. viewing
on the screen or processing it for some purpose). There should
definitively be a Comparable instance that can be used as a Map key for
the different orders supported. Definitively use specific classes for
keys instead of abusing toString() for this!
Cheers
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-07 06:00 -0700
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 09:44 -0400
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 11:33 -0400
Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-07 10:22 -0700
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 14:16 -0400
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 14:36 -0400
Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-07 12:13 -0700
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 16:00 -0400
Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 08:20 +0000
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 06:57 -0400
Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 11:08 +0000
Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 11:22 +0000
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 07:45 -0400
Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 14:12 +0000
Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-08 05:50 -0700
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 09:23 -0400
Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-08 06:47 -0700
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 10:31 -0400
Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-08 08:24 -0700
Re: Is there a better way to group and sort objects? Robert Klemme <shortcutter@googlemail.com> - 2018-08-14 14:06 +0200
Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-14 05:49 -0700
Re: Is there a better way to group and sort objects? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2018-08-09 16:06 +0200
Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-14 05:47 -0700
csiph-web