Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38523
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Is there a better way to group and sort objects? |
| Date | 2018-08-07 14:16 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <pkcnmi$ioq$1@dont-email.me> (permalink) |
| References | <8c045ff4-98c5-4253-9136-b26b34cf8f77@googlegroups.com> <pkc7nj$6du$1@dont-email.me> <fbdc6564-aa69-4db8-9e90-393ea9469f05@googlegroups.com> |
On 8/7/2018 1:22 PM, Eric Douglas wrote:
> On Tuesday, August 7, 2018 at 9:44:30 AM UTC-4, Eric Sosman wrote:
>> On 8/7/2018 9:00 AM, Eric Douglas wrote:
>>> I want to combine and sort objects by key properties and merge their secondary properties. Is there a better way? This is what I came up with..
>>>
>>> public abstract class TestStatic {
>>> private static class TestClass extends Object implements Comparable<Object> {
>>> private int var1;
>>> private int var2;
>>>
>>> @Override
>>> public int compareTo(Object compareMe) {
>>> if (compareMe == null || compareMe.getClass() != getClass()) {
>>> return 1;
>>> }
>>> return var1 - ((TestClass) compareMe).getVar1();
>>> }
>>
>> Note that this method violates the Comparable contract. If you use
>> this compareTo() method with Arrays.sort() or Collections.sort() or a
>> SortedMap or whatever, and the objects being compared are a mixture of
>> different TestClass subclasses, there's no telling what might happen.
>>
>
> While in my real code which follows this structure it makes no sense to ever compare this to a null or instance of different class, I just want it to do whatever stupid comparison makes sense in that case. Would splitting the class condition make more sense?
> if (compareMe == null) {
> return 1;
> }
> if (compareMe.getClass() != getClass()) {
> return Integer.compare(hashCode(),compareMe.hashCode());
> }
It still fails to be antisymmetric:
TestClass obj = new TestClass();
TestClass nil = null;
int one = obj.compareTo(nil); // yields +ve
int two = nil.compareTo(obj); // should yield -ve, but throws
(This discussion seems oddly familiar: Have we had it before? Ah, yes:
Google Groups finds a thread from two years ago titled "Why not String
compare?" and another from one year back titled "What's the best way to
make a cross string set?" Both threads discuss implementing Comparable
and/or Comparator, and the latter touches upon the impossibility of
implementing Comparable<Object>. Will this query be an annual event? :)
>> Alternatively, you might take advantage of the fact that Map::put
>> returns the old key (if any) associated with its value:
Aside: Um, er, Map::put returns the old *value* (if any) associated
with its *key*, yeah, that's what I meant, yeah, obviously, of course.
"I misspoke," as the President so eloquently and so often says.
>>
>> TestClass test2 = new TestClass();
>> test2.setVar1(4);
>> TestClass oldTest = hm.put(test2.toString(), test2);
>> test2.setVar2(42 + (oldTest == null ? 0 : oldTest.getVar2()));
>>
>> ... thus cutting the number of map searches in half.
> In one real world implementation the objects represent contacts (people). The keys are name, phone number, and email address. These people merge in from different sources. Each source is a distinct field. One source calls setShipToContact(true), one calls .setMailToContact(true), another calls .setBillToContact(true). While each may only have to call one distinct method, it may be more efficient for all to call all methods to copy from object 1 to object 2 when a duplicate is found, than to ask the map .contains then tell the map to .put.
Using the value returned by put() saves one map lookup in the case
when the key is already present. (When I wrote "in half" I was being
over-optimistic about the likelihood of duplicates.)
What you do after discovering the duplication is your problem, no
matter how you manage the map. If you intend to "copy" one value's
state to the other, it's not clear (from what you've revealed) why
you bother "merging" at all. If there's some kind of "merge" or
"reconciliation" to be done, it's up to you to do it.
>>> TestClass test3 = new TestClass();
>>> test3.setVar1(3);
>>> if (hm.containsKey(test3.toString())) {
>>> test3 = hm.get(test3.toString());
>>> test3.setVar2(test3.getVar2() + 55);
>>> } else {
>>> test3.setVar2(55);
>>> hm.put(test3.toString(), test3);
>>> }
>>
>> Similarly here.
>
> What am I missing here?
Same suggestion: Ditch containsKey() and let put() tell you
whether the new key is unique or a duplicate, then "copy" or "merge"
or whatever as before.
--
esosman@comcast-dot-net.invalid
Eight hundred ninety-seven days to go.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-07 06:00 -0700
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 09:44 -0400
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 11:33 -0400
Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-07 10:22 -0700
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 14:16 -0400
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 14:36 -0400
Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-07 12:13 -0700
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 16:00 -0400
Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 08:20 +0000
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 06:57 -0400
Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 11:08 +0000
Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 11:22 +0000
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 07:45 -0400
Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 14:12 +0000
Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-08 05:50 -0700
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 09:23 -0400
Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-08 06:47 -0700
Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 10:31 -0400
Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-08 08:24 -0700
Re: Is there a better way to group and sort objects? Robert Klemme <shortcutter@googlemail.com> - 2018-08-14 14:06 +0200
Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-14 05:49 -0700
Re: Is there a better way to group and sort objects? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2018-08-09 16:06 +0200
Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-14 05:47 -0700
csiph-web