Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37067
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: What's the best way to make a cross string set? |
| Date | 2017-08-05 16:08 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <om58cc$2bd$1@dont-email.me> (permalink) |
| References | (12 earlier) <d6795819-3c8a-464a-8ce1-ba47779d80dd@googlegroups.com> <olvi83$kku$1@dont-email.me> <fa610c79-f1b6-43b6-85fa-ffc4ca5298d4@googlegroups.com> <olvn2h$9kv$1@dont-email.me> <abafc97c-13c8-4934-9f55-0e0606c126c7@googlegroups.com> |
[Re-sending; earlier follow-up vanished into the ether.]
On 8/3/2017 2:17 PM, Eric Douglas wrote:
> [...]
> It is groupings, not just comparing Object to Object which would have
issues with comparing a String to an Integer.
Not sure what you mean by "it is groupings." In any event,
both Comparable::compareTo and Comparator::compare perform a
pairwise comparison.
> It's like this.
> public class MyComparableClass implements Comparable<MyCustomClass> {
> private short style;
> public final short sSTYLE = 0;
> public final short iSTYLE = 1;
Aside: Why are these not static?
> private String sValue;
> private int iValue;
> public void setStyle(final short newstyle){
> style=newstyle;
> }
> public Short getStyle(){
> return style;
> }
> public void setString(final String value){
> sValue=value;
> }
> public String getString(){
> return sValue;
> }
> public void setInt(final int value){
> iValue=value;
> }
> public Integer getInt(){
> return iValue;
> }
> protected Comparable<Object getCompareValue(){
Aside: Missing a >, I think.
> Object compareValue;
> switch(style){
> case sSTYLE:
> compareValue=sValue;
> break;
> case iSTYLE:
> compareValue=iValue;
> break;
> }
> return (Comparable<Object>)compareValue;
Aside: Compile error here.
> }
> public int compareTo(final MyComparableClass o){
> final Comparable<Object> compareMe = getCompareValue();
> final Comparable<Object> compareMeTo = o.getCompareValue();
> if (getStyle().equals(o.getStyle())){
> return compareMe.compareTo(compareMeTo);
> }
> return getStyle().compareTo(o.getStyle());
Crux: This is going "all the way around Robin Hood's barn"
to accomplish something that could be done in a far simpler way,
such as
class Thing implements Comparable<Thing> {
// ... statics (and noise)
private short style;
private String sValue;
private int iValue;
// ... constructors, getters, setters (and spaniels)
@Override
public int compareTo(Thing that) {
int result = this.style - that.style;
if (result == 0) {
switch (style) {
case sStyle:
result = this.sValue.compareTo(that.sValue);
break;
case iStyle:
result = Integer.compare(
this.iValue, that.iValue);
break;
default:
throw new IllegalStateException(
"poor style: " + style;
}
}
return result;
}
}
... and that's all: A simple multi-key comparison. None of this
nonsense about pretending to be Comparable<EverythingImaginable>,
which of course isn't possible to do in a sensible way.
[The above is from two days ago; here's an addendum:]
Upon reflection, strike "in a sensible way" and replace with
"at all, without violating the contract of Comparable." The
problem is that Comparable must implement a /total order/, which
means (among other things) that it must be asymmetric: The sign
of x.compareTo(y) must be opposite that of y.compareTo(x), or
both must be zero. Now, you may contrive a class that claims to
implement Comparable<Object> and endow it with a compareTo()
method that does, in fact, accept any kind of Object and return
some kind of result:
class MyStrangeClass implements Comparable<Object> {
public int compareTo(Object obj) { ... }
...
}
MyStrangeClass x = ...
JButton y = new JButton("DO NOT PRESS");
int xy = x.compareTo(y);
Fine so far, but then
int yx = y.compareTo(x);
... won't even compile, much less return a result whose sign is
opposite that of xy. You can't implement Comparable<Object>
properly unless you make *every* class implement it, and make
them all implement it consistently (that is, if a particular
Random compares greater than a specific OutputStream, the
OutputStream must also compare less than the Random). Good
luck with that ...
(Andreas Leitgeb wrote about this briefly two days ago;
it's only now begun to penetrate my thick skull.)
--
esosman@comcast-dot-net.invalid
Twelve hundred sixty-four days to go.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-27 12:12 -0700
Re: What's the best way to make a cross string set? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-07-27 14:38 -0500
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-27 12:49 -0700
Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-27 15:49 -0400
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-27 12:55 -0700
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-27 13:22 -0700
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-27 13:30 -0700
Re: What's the best way to make a cross string set? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-07-27 15:34 -0500
Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-27 18:27 -0400
Re: What's the best way to make a cross string set? lewbloch@gmail.com - 2017-07-28 18:51 -0700
Re: What's the best way to make a cross string set? v_borchert@despammed.com (Volker Borchert) - 2017-07-29 06:03 +0000
Re: What's the best way to make a cross string set? v_borchert@despammed.com (Volker Borchert) - 2017-07-28 18:19 +0000
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-28 13:28 -0700
Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-28 17:20 -0400
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-31 05:24 -0700
Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-31 12:01 -0400
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-31 10:58 -0700
Re: What's the best way to make a cross string set? Sebastian <nowhere@nirvana.org> - 2017-07-31 18:13 +0200
Re: What's the best way to make a cross string set? lewbloch@gmail.com - 2017-07-31 09:24 -0700
Re: What's the best way to make a cross string set? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-31 19:24 -0400
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-01 06:36 -0700
Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-01 10:58 -0400
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-01 08:09 -0700
Re: What's the best way to make a cross string set? lewbloch@gmail.com - 2017-08-01 09:39 -0700
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-01 10:18 -0700
Re: What's the best way to make a cross string set? Andreas Leitgeb <avl@logic.at> - 2017-08-02 09:22 +0000
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-02 05:13 -0700
Re: What's the best way to make a cross string set? Andreas Leitgeb <avl@logic.at> - 2017-08-03 14:33 +0000
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-03 08:51 -0700
Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-03 12:20 -0400
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-03 10:15 -0700
Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-03 13:42 -0400
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-03 11:17 -0700
Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-05 16:08 -0400
Re: What's the best way to make a cross string set? Martin Gregorie <martin@address-in-sig.invalid> - 2017-08-05 20:31 +0000
Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-05 17:25 -0400
Re: What's the best way to make a cross string set? Martin Gregorie <martin@address-in-sig.invalid> - 2017-08-05 22:11 +0000
Re: What's the best way to make a cross string set? Sebastian <nowhere@nirvana.org> - 2017-08-06 18:13 +0200
Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-02 12:56 -0700
Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-29 16:55 -0400
Re: What's the best way to make a cross string set? Sebastian <nowhere@nirvana.org> - 2017-07-29 23:56 +0200
csiph-web