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


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

Re: Is there a better way to group and sort objects?

Newsgroups comp.lang.java.programmer
Date 2018-08-07 12:13 -0700
References <8c045ff4-98c5-4253-9136-b26b34cf8f77@googlegroups.com> <pkc7nj$6du$1@dont-email.me> <fbdc6564-aa69-4db8-9e90-393ea9469f05@googlegroups.com> <pkcnmi$ioq$1@dont-email.me>
Message-ID <31b0fbe7-3e14-4b60-9779-8435fcd04e22@googlegroups.com> (permalink)
Subject Re: Is there a better way to group and sort objects?
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


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

Ah ok, in this case there should never be a null.  I keep thinking when a method passes in any sort of Object I should account for null as some possibility.  I believe you're suggesting the compareTo method just start with this:
if (compareMe == null) {
throw new Error("don't compare me to a null!");
}

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

Right?  I believe we both said the same thing and meant something else.  The return value of the put method is the value of the key at the put position (which may be null).  I'm just using my toString() to manually look for a matching object, because I don't otherwise care about the map keys but if there is a matching object, I need to get some values from it, and I don't know how else to get it.  At first I was going to use HashSet, but while it can add() and it can check contains() I didn't see a get value method.

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

Ah, I just put 3 objects in my example to show that it stored one, stored a second one separately, and merged another one using the same if condition that stored the second one.  Of course if we change that one condition we change the other to match.  I thought you were saying there's a better way to get the sorted objects out from the last part:

ArrayList<TestClass> al = new ArrayList<>();
al.addAll(hm.values());
Collections.sort(al);
for (TestClass t1 : al) {
	System.out.println(t1.getVar1() + " " + t1.getVar2());
}

If there are no other ideas, thanks for the tip that this code:
if (hm.containsKey(test2.toString())) {
	test2 = hm.get(test2.toString());
	test2.setVar2(test2.getVar2() + 42);
} else {
	test2.setVar2(42);
	hm.put(test2.toString(), test2);
}

works the same as this code (but this should be more efficient).
TestClass temp = hm.put(test2.toString(),test2);
test2.setVar2(temp == null ? 42 : temp.getVar2() + 42);

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


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