Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: What's the best way to make a cross string set? Date: Sat, 5 Aug 2017 16:08:36 -0400 Organization: A noiseless patient Spider Lines: 142 Message-ID: References: <6f9ff338-1944-4b64-83b8-4a921ce57a89@googlegroups.com> <69ec0210-e2a0-4823-98f0-8fc72e5c5db9@googlegroups.com> <3664ab4d-9619-43d0-9535-1ce7ab4d4438@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 5 Aug 2017 20:04:28 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="748a76b6fedd1c9675e9d2f9cc26a9df"; logging-data="2413"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX181PzJpqkHOmYthACQimvpo" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 In-Reply-To: Content-Language: en-US Cancel-Lock: sha1:cTQ1/pYwQ82Z/x8XsFMAx//5xV4= Xref: csiph.com comp.lang.java.programmer:37067 [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 { > 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, I think. > Object compareValue; > switch(style){ > case sSTYLE: > compareValue=sValue; > break; > case iSTYLE: > compareValue=iValue; > break; > } > return (Comparable)compareValue; Aside: Compile error here. > } > public int compareTo(final MyComparableClass o){ > final Comparable compareMe = getCompareValue(); > final Comparable 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 { // ... 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, 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 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 { 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 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.