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


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

Re: Comparison structure?

X-Received by 10.129.77.87 with SMTP id a84mr1240090ywb.30.1475602176443; Tue, 04 Oct 2016 10:29:36 -0700 (PDT)
X-Received by 10.157.4.137 with SMTP id 9mr369625otm.3.1475602176398; Tue, 04 Oct 2016 10:29:36 -0700 (PDT)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!g45no281788qte.1!news-out.google.com!203ni6887itk.0!nntp.google.com!o19no325887ito.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.java.programmer
Date Tue, 4 Oct 2016 10:29:36 -0700 (PDT)
In-Reply-To <vin7vbtorg7k171plq29ve3fo4fut237d5@4ax.com>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=50.78.95.177; posting-account=2czF5goAAAD4GBMPIGV4KcD2K4PhoB_H
NNTP-Posting-Host 50.78.95.177
References <59c17fe5-61a6-46dc-918f-ee5aa348e3fd@googlegroups.com> <vin7vbtorg7k171plq29ve3fo4fut237d5@4ax.com>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <cfb773de-671a-46e7-9d79-e821780c7bcb@googlegroups.com> (permalink)
Subject Re: Comparison structure?
From Eric Douglas <e.d.programmer@gmail.com>
Injection-Date Tue, 04 Oct 2016 17:29:36 +0000
Content-Type text/plain; charset=UTF-8
Xref csiph.com comp.lang.java.programmer:35714

Show key headers only | View raw


On Tuesday, October 4, 2016 at 12:53:51 PM UTC-4, Roedy Green wrote:
> >Is there a better structure?
>  the only alternative I can think of is to use a compare method that
> returns +1 -1 0.
> Then you could use a switch.

In that case you'd have to override the compareTo?
I read the doc on that method and it says returns a zero, or a value less than or greater than zero.  If it's not guaranteed to return specific values for less than or greater than, what would be the switch case?
This seems at least one reason people in the following thread suggest sticking with the 3-way if.
http://stackoverflow.com/questions/18510360/how-to-use-greater-than-or-equal-in-a-switch-statement

Otherwise that code would be equivalent to
          Integer compareMe = 1;
          int compareTo = 2;
          final int assignMe;
          switch (compareMe.compareTo(compareTo)) {
          case 0:
               assignMe = 4;
               break;
          case 1:
               assignMe = compareMe + 2;
               break;
          case -1:
               assignMe = compareMe;
               break;
          }

Of course Integer is final so you couldn't even just subclass it to override compareTo.  Without having 3 certain return values, this method ends up uglier.

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


Thread

Comparison structure? Eric Douglas <e.d.programmer@gmail.com> - 2016-10-04 07:25 -0700
  Re: Comparison structure? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-04 10:46 -0400
  Re: Comparison structure? Roedy Green <inquiry@mindprod.com> - 2016-10-04 09:53 -0700
    Re: Comparison structure? Eric Douglas <e.d.programmer@gmail.com> - 2016-10-04 10:29 -0700
      Re: Comparison structure? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-04 15:35 -0400
        Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-04 20:16 -0400
          Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-04 22:29 -0400
            Re: Comparison structure? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-05 08:08 -0400
              Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-05 20:27 -0400
  Re: Comparison structure? Martin Gregorie <martin@address-in-sig.invalid> - 2016-10-04 17:37 +0000
    Re: Comparison structure? Eric Douglas <e.d.programmer@gmail.com> - 2016-10-04 12:02 -0700
    Re: Comparison structure? j4n bur53 <janburse@fastmail.fm> - 2016-10-05 17:12 +0200
      Re: Comparison structure? j4n bur53 <janburse@fastmail.fm> - 2016-10-05 17:14 +0200
  Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-04 20:03 -0400
  Re: Comparison structure? Marcel Mueller <news.5.maazl@spamgourmet.org> - 2016-10-05 22:41 +0200

csiph-web