Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35472

Re: Why not String compare?

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Why not String compare?
Date 2016-08-26 14:50 -0400
Organization A noiseless patient Spider
Message-ID <npq328$a0b$1@dont-email.me> (permalink)
References <33463070-424f-4fb0-956b-2849f1a400eb@googlegroups.com> <nppu0n$nle$1@dont-email.me> <855574dd-f21f-46f9-8972-a55aa189cf27@googlegroups.com>

Show all headers | View raw


On 8/26/2016 1:48 PM, Eric Douglas wrote:
> On Friday, August 26, 2016 at 1:24:48 PM UTC-4, Eric Sosman wrote:
>>      Are you talking about Comparator, or about Comparable?  The
>> latter has a compareTo() method, the former does not.
>>
>>      If you're speaking of Comparable, I think you're in violation
>> of the spec.  In
>>
>> 	ComparableThing x = ...;  // non-null
>> 	ComparableThing y = null;
>> 	int xy = x.compareTo(y);
>> 	int yx = y.compareTo(x);
>>
>> ... you don't get xy and yx with opposite signs, as required by
>> the Comparable documentation:
>>
>> 	The implementor must ensure sgn(x.compareTo(y)) ==
>> 	-sgn(y.compareTo(x)) for all x and y. (This implies that
>> 	x.compareTo(y) must throw an exception iff y.compareTo(x)
>> 	throws an exception.)
>>
>>      If you're speaking of Comparator, simply returning 1 when either
>> argument of compare() is null is also a breach.  Consider
>>
>> 	Thing x = ...;  // non-null
>> 	Thing y = null;
>> 	Comparator<Thing> c = ...;
>> 	int xy = c.compare(x, y);
>> 	int yx = c.compare(y, x);
>>
>> Again, you must ensure that sgn(xy) = -sgn(yx), but if both are 1
>> this doesn't hold.  (You should also arrange that c.compare(null,null)
>> returns 0, not 1 or -1.)
>
> Not trying to reverse it, why would I need to reverse it?

     Um, er, to satisfy the contract of Comparable?

> I have a TreeSet called records, and I do this.
> records.stream().filter(item -> item.getKey(currKnum).compareTo(currKey) > 0)
>
> The getKey() method of the objects in this TreeSet accepts an int and returns a (non-null)String. I want this filter to return all items.  What better way than to pass in null as the value to compareTo?  Shouldn't everything be greater than null?

     When you want all the items, not filtering at all strikes me as
a "better way."

     An "Eric Douglas" has many times exhibited in this newsgroup a
preoccupation with efficiency.  Clearly, the "Eric Douglas" who runs
an entire TreeSet through a no-op filtering stream has a radically
different world view.

-- 
esosman@comcast-dot-net.invalid
The Tooth Fairy has gone high-tech, now pays in Bitecoin.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 09:00 -0700
  Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-08-26 18:49 +0200
    Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 10:09 -0700
      Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-08-26 22:48 +0200
        Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 13:55 -0700
          Re: Why not String compare? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-26 17:29 -0400
          Re: Why not String compare? Lew <lewbloch@gmail.com> - 2016-08-30 22:20 -0700
          Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-08-31 08:17 +0200
            Re: Why not String compare? Andreas Leitgeb <avl@logic.at> - 2016-08-31 09:45 +0000
              Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-08-31 19:24 +0200
                Re: Why not String compare? Andreas Leitgeb <avl@logic.at> - 2016-09-01 10:17 +0000
                Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-09-02 18:29 +0200
  Re: Why not String compare? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-26 13:24 -0400
    Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 10:48 -0700
      Re: Why not String compare? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-26 14:50 -0400
        Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 12:19 -0700
      Re: Why not String compare? Lothar Kimmeringer <news200709@kimmeringer.de> - 2016-09-01 20:42 +0200
        Re: Why not String compare? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-09-01 17:57 -0400
    Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 11:03 -0700
      Re: Why not String compare? Lothar Kimmeringer <news200709@kimmeringer.de> - 2016-09-02 21:54 +0200

csiph-web