From: Eric Sosman on
On 4/24/2010 12:57 PM, Mike Schilling wrote:
> 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.

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

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Tom Anderson on
On Sat, 24 Apr 2010, Mike Schilling wrote:

> 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>>()
> {... };

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

tom

--
Any problem in computer science can be solved with another layer of
indirection. But that usually will create another problem. -- David
Wheeler
From: Lew on

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

Tom Anderson wrote:
> 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.

--
Lew
From: Mike Schilling on
Mike Schilling wrote:
> 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.

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