From: Lew on
Mike Schilling wrote:
> Lew wrote:
>> Mike Schilling wrote:
>>> Lew wrote:
>>>> Feed a
>>>> man a meal and you feed him for an hour; send him to the diner and
>>>> you feed him for a lifetime.
>>> Build a man a fire, and he's warm for a night, set him on fire, and
>>> he's warm his whole life.
>>>
>>>>> I do use a TreeMap --
>>>>> "final Map<String, String> map = new
>>>>> TreeMap<String, String>();",
>>>>> but the order is not getting preserved when I call "map.entrySet".
>>>> And you didn't even provide an SSCCE! What is wrong with you?
>>> Since he wanted to sort by value, not key, he's correct -- the entry
>>> set won't be sorted the way he wanted.
>> Unless, of course, he uses a custom Comparator that does sort the way
>> he wants.
>
> 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.

--
Lew
From: Eric Sosman on
On 4/24/2010 2:36 AM, Mike Schilling wrote:
> [...]
> 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.

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Mike Schilling on
Eric Sosman wrote:
> On 4/24/2010 2:36 AM, Mike Schilling wrote:
>> [...]
>> 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.


From: Mike Schilling on
Lew wrote:
> Mike Schilling wrote:
>> Lew wrote:
>>> Mike Schilling wrote:
>>>> Lew wrote:
>>>>> Feed a
>>>>> man a meal and you feed him for an hour; send him to the diner and
>>>>> you feed him for a lifetime.
>>>> Build a man a fire, and he's warm for a night, set him on fire, and
>>>> he's warm his whole life.
>>>>
>>>>>> I do use a TreeMap --
>>>>>> "final Map<String, String> map = new
>>>>>> TreeMap<String, String>();",
>>>>>> but the order is not getting preserved when I call
>>>>>> "map.entrySet".
>>>>> And you didn't even provide an SSCCE! What is wrong with you?
>>>> Since he wanted to sort by value, not key, he's correct -- the
>>>> entry set won't be sorted the way he wanted.
>>> Unless, of course, he uses a custom Comparator that does sort the
>>> way he wants.
>>
>> 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.

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


From: Mike Schilling on
Tom Anderson wrote:
>
> 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>>()
{... };