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


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

Re: Why not String compare?

From Lothar Kimmeringer <news200709@kimmeringer.de>
Newsgroups comp.lang.java.programmer
Subject Re: Why not String compare?
Date 2016-09-01 20:42 +0200
Organization Organization?! Only chaos here!
Message-ID <1pr3rgydxxzp5.dlg@kimmeringer.de> (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


Eric Douglas wrote:

> Not trying to reverse it, why would I need to reverse it?

Maybe not you but the common use-case that uses compareTo

> 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?

compareTo is commonly used for sorting data. So a.compareTo(b)
is called to decide what element should be sorted to "the left"
and what element should be sorted to "the right". What element
is a and b is dependent on the implementation of the sorting
algortithm and the actual data within the set.

Allowing a.compareTo(null) to return something would lead to
non-deterministic behavior since the sort might succeed if by
chance null ends up being b all the time. But an optimization
of the sorting algorithm or a new element in the set might
lead to null end up as a and referencing null always leads to
a NPE. Throwing a NPE when compareTo is called with null as
parameter brings back the deterministic behavior and therefor
is declared as requirement for implementations of compareTo.


Cheers, Lothar
-- 
Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
               PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                 questions!

--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

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