Way to sort / enforce order for Map.entrySet?

T

Tom Anderson

I recommend "Loose Cannon" beer from Clipper City Beer in Baltimore,
Maryland, USA. <www.clippercitybeer.com> and not merely because
"lewscanon.com" was a pun on that expression.

Loose Cannon is very, very hoppy to the point of tasting like a citrus
beverage. That took some getting used to, but by the second glass it
was a favorite.

I think this is an American thing. In the fusty old Old World, we tend to
consider such things unbalanced. But over there, you guys love taking one
particular knob and turning it up to 11.

I mostly drink London Pride, because it's local, easily available, and
alright.

I have a bottle of this in my cupboard, because it's local to where i grew
up, should be alright (their other beers are good), and is carbon-neutral:

http://about.adnams.co.uk/post/News/2008/08/East-Green.aspx

Which i thought was an enterprising marketing angle.

tom
 
T

Tom Anderson

Mind you, I have no objection to providing code to help the OP.

I do have a real objection to one ignoring advice that lacks every last
freaking detail of the code and prevents the querent from having to do
any work whatsoever. The OP pretended that your answer came out of
nowhere, and that no one else had suggested the same thing, as if the
lack of code made the other answer(s) any less valuable.

Yes, that was an unacceptable snub.
If they really didn't see the value of being told, "Custom Comparator.
Load the entries into a SortedSet with that Comparator." then they
should have *asked* about it. Then when you gave the code for that (I
assume that's what they were really answering, not the post to which
they actually replied) they pretended like you were the first to even
suggest such a thing in answer to their question.

You may be right that laredotornado is just too lazy to do his own
programming.

I have a terrible vice of optimism. I think that for someone who doesn't
know about the collections API, and hasn't built up a general
understanding of the 'Tao of Java', if you will, that will let them grok
it from reading the docs, advice such as you gave, correct though it is,
might as well be in Greek. In that case, a picture is worth a thousand
words, and in programming a picture is a snippet.

Imagine if you were learning to maintain tractors, and you asked on a
tractor newsgroup about how to fix a noisy second gear, and someone said
"Tappet rod shim. Run the treadle via a coupling cone using that shim.",
would you be confident in going off to do that? Would you even know what a
sensible question to ask in response was? On the other hand, if someone
gave you detailed instructions, or ideally showed you how to do it, you'd
be able to do it, and if you were a smart person, you'd be looking at that
and figuring out how it works, and so learning how to do something similar
in future. I think there's enormous educational value in examples.
laredotornado, you are not going to make real progress in computer
programming until you grasp how to take a topic suggestion into code.

Yes. The theory advanced in the preceding paragraph hinges on "if you were
a smart person", and that's what's not clear in the OP's case. If this guy
is still around in two years asking the same entry-level questions, then
the theory will have been disproven, and i ought to start leaving him to
stew.
There are wise people out there, for example Roedy Green, who will give
you a signpost to the restaurant instead of a sandwich. Feed a man a
meal and you feed him for an hour; send him to the diner and you feed
him for a lifetime.

Send him to a diner in Bulgaria and he'll still starve, because he doesn't
know what to order.

tom
 
L

Lew

Mike said:
Not Map.entrySet(); that always sorts by the map's keys. (You can *copy*
the result of entrySet() and sort that copy however you like, of course.)

And that would be exactly the advice that several people gave him.
 
E

Eric Sosman

[...]
Not Map.entrySet(); that always sorts by the map's keys. (You can *copy*
the result of entrySet() and sort that copy however you like, of course.)

Two points to add: First, the Set returned by entrySet() only
"sorts" the entries if the underlying Map implementation makes a
guarantee of some kind. Not all do, and LinkedHashMap (at least)
promises[*] an order that is not based on the keys. Second, a
Map.Entry object is only valid as long as the Map from which it
was derived remains unchanged. Modify the Map, and any existing
Map.Entry instances become so much junk.

[*] Actually, the Javadoc for LinkedHashMap says only that
its iterator() method produces Iterators that visit entries in
non-key order. The order of an Iterator on its entrySet() is not
explicitly described, so I'm really only guessing that it might
be the same.
 
M

Mike Schilling

Eric said:
[...]
Not Map.entrySet(); that always sorts by the map's keys. (You can
*copy* the result of entrySet() and sort that copy however you like,
of course.)

Two points to add: First, the Set returned by entrySet() only
"sorts" the entries if the underlying Map implementation makes a
guarantee of some kind.

Right. In the part you snipped, it was stated explicitly that the
underlying map was a TreeMap.
 
M

Mike Schilling

Lew said:
And that would be exactly the advice that several people gave him.

My point is that it doesn't require an SSCCE to demonstrate the the
entrySet() of a TreeMap isn't sorted by the values.
 
M

Mike Schilling

Tom said:
I have a terrible vice of optimism. I think that for someone who
doesn't know about the collections API, and hasn't built up a general
understanding of the 'Tao of Java', if you will, that will let them
grok it from reading the docs, advice such as you gave, correct
though it is, might as well be in Greek. In that case, a picture is
worth a thousand words, and in programming a picture is a snippet.

Good Lodr, Tom, are you really suggesting that it isn't intuitively obvious
to the rawest beginner that the new set should be created via

SortedSet<Map.Entry<String, String>> results =
new TreeSet<Map.Entry<String, String>>(
new Comparator <Map.Entry<String, String>>()
{... };
 
E

Eric Sosman

Eric said:
[...]
Not Map.entrySet(); that always sorts by the map's keys. (You can
*copy* the result of entrySet() and sort that copy however you like,
of course.)

Two points to add: First, the Set returned by entrySet() only
"sorts" the entries if the underlying Map implementation makes a
guarantee of some kind.

Right. In the part you snipped, it was stated explicitly that the
underlying map was a TreeMap.

Right. TreeMap.entrySet().iterator() traverses the Map.Entry's
in order by keys. In general, though, Map.entrySet().iterator()
makes no such promise.
 
T

Tom Anderson

Good Lodr, Tom, are you really suggesting that it isn't intuitively obvious
to the rawest beginner that the new set should be created via

SortedSet<Map.Entry<String, String>> results =
new TreeSet<Map.Entry<String, String>>(
new Comparator <Map.Entry<String, String>>()
{... };

You're right, Mike, absurd, what was i thinking.

tom
 
L

Lew

Tom said:
You're right, Mike, absurd, what was i thinking.

You know, once one is told that SortedSet and a custom Comparator are needed,
it's really quite easy to go to the Javadocs for those classes and look up how
they work, and then not very difficult to figure out to do just that.

If one is taking responsibility for their own skills development, that is.
Otherwise it's back to spoon-fed pablum and good effing luck.

Sooner or later anyone who wants to be any good as a programmer is going to
have to figure out how to do that sort of thing, or they're never going to
advance.
 
M

Mike Schilling

Mike said:
My point is that it doesn't require an SSCCE to demonstrate the the
entrySet() of a TreeMap isn't sorted by the values.

Particularly since (it belatedly occurs to me), there's no requirement that
the values be comparable to each other.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,844
Latest member
JudyGvh32

Latest Threads

Top