Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38526
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Is there a better way to group and sort objects? |
| Date | 2018-08-07 16:00 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <pkctpm$rf5$1@dont-email.me> (permalink) |
| References | <8c045ff4-98c5-4253-9136-b26b34cf8f77@googlegroups.com> <pkc7nj$6du$1@dont-email.me> <fbdc6564-aa69-4db8-9e90-393ea9469f05@googlegroups.com> <pkcnmi$ioq$1@dont-email.me> <31b0fbe7-3e14-4b60-9779-8435fcd04e22@googlegroups.com> |
On 8/7/2018 3:13 PM, Eric Douglas wrote:
>> It still fails to be antisymmetric:
>>
>> TestClass obj = new TestClass();
>> TestClass nil = null;
>> int one = obj.compareTo(nil); // yields +ve
>> int two = nil.compareTo(obj); // should yield -ve, but throws
>>
>> (This discussion seems oddly familiar: Have we had it before? Ah, yes:
>> Google Groups finds a thread from two years ago titled "Why not String
>> compare?" and another from one year back titled "What's the best way to
>> make a cross string set?" Both threads discuss implementing Comparable
>> and/or Comparator, and the latter touches upon the impossibility of
>> implementing Comparable<Object>. Will this query be an annual event? :)
>>
> Ah ok, in this case there should never be a null.
... in which case, it's hard to understand why you check for it.
> I keep thinking when a method passes in any sort of Object I should account for null as some possibility. I believe you're suggesting the compareTo method just start with this:
> if (compareMe == null) {
> throw new Error("don't compare me to a null!");
> }
I *think* that's all right. Since nil.compareTo(obj) will throw,
obj.compareTo(nil) must also throw -- but I don't see a requirement
that it throw the same Throwable type. An `assert' would also be
okay, I think, because (1) if it's enabled and throws AssertionError
that satisfies the requirement to throw, and (2) if it's disabled
you'll get NullPointerException as soon as you try to access the
fields or methods of the null reference. Objects.requireNonNull()
is yet another possibility.
What you must not do is what you wrote originally: Pretend the
null reference is to a "null object" and "compare to" that thing.
The null reference is to "no object at all," and you cannot compare
to "no object at all" -- unless "no object at all" can compare itself
to your object, and consistently.
>>>> Alternatively, you might take advantage of the fact that Map::put
>>>> returns the old key (if any) associated with its value:
>>
>> Aside: Um, er, Map::put returns the old *value* (if any) associated
>> with its *key*, yeah, that's what I meant, yeah, obviously, of course.
>> "I misspoke," as the President so eloquently and so often says.
>
> Right? I believe we both said the same thing and meant something else. The return value of the put method is the value of the key at the put position (which may be null).
No. That's what I wrote initially, and that mis-statement is what
I corrected above. (Dyxlesia is a dyslexic's self-diangosis.)
--
esosman@comcast-dot-net.invalid
Eight hundred ninety-seven days to go.
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