DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

TreeMultimap first(), last() methods #1467

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Using the TreeMultimap as a Multimap equivalent to a TreeMap is extremely 
useful. However, it isn't quite as useful when it comes to getting the first, 
last entries etc. 

Currently, to get the first values, the code is:
SortedSet<Object> objs = map.get(map.keySet().first());

To get the last values:
SortedSet<Object> objs = map.get(map.keySet().last());

It would be very useful if this type of logic could be exposed as an API on the 
TreeMultimap class or even on the SortedMultimap API. This would simplify the 
above examples to be:

SortedSet<Object> first = map.first();
SortedSet<Object> last = map.last();

There could also be scope to extend this to provide equivalent functionality 
for other methods, such as headMap, tailMap etc. 

These methods would be extremely useful, especially as the TreeMultiset is a 
sorted map; these methods are half the reason why I use a sorted map in the 
first place.

Original issue reported on code.google.com by richardd...@gmail.com on 3 Jul 2013 at 1:21

GoogleCodeExporter commented 9 years ago
Even NavigableMap doesn't have first/last methods returning a value. There's 
firstKey(), lastKey(), firstEntry() and lastEntry().

firstKey() and lastKey() don't seem to serve much purpose when you can do 
keySet().first() and keySet().last(). And firstEntry()/lastEntry() don't 
translate well to a Multimap, where the entries of a Multimap are single 
key/value pairs (not key/collection<value> pairs).

(Also, "SortedSet<Object> objs = map.get(map.keySet().first());" doesn't look 
that bad to me.)

Louis, any thoughts?

Original comment by cgdecker@google.com on 3 Jul 2013 at 2:54

GoogleCodeExporter commented 9 years ago
I searched the Google code base for 'get[(].*keySet[(][)].*(first|last)[(][)]' 
and got no results. I do suspect that TreeMultimap is underused, so probably 
there *ought* to be a few callers. Still, it's sounding like this comes up more 
frequently for you than for most users.

Hmm, I guess there's another way to do this:
multimap.asMap().firstEntry().getValue()

That's pretty well in line with what you'd need to do with a NavigableMap, as 
Colin noted, and it's the kind of thing that asMap() is there for.

(A search for 'asMap.*(first|last)Entry.*getValue' does turn up a couple 
results, albeit in tests that might be better served by getOnlyElement or by 
assertions against the contents of the multimap as a whole.)

Original comment by cpov...@google.com on 3 Jul 2013 at 3:19

GoogleCodeExporter commented 9 years ago
> multimap.asMap().firstEntry().getValue()

Of course, you'd have to either be ok with the result being a Collection<V> or 
do a cast to SortedSet<V>.

Original comment by cgdecker@google.com on 3 Jul 2013 at 3:22

GoogleCodeExporter commented 9 years ago
That's definitely how I'd do it, yeah.  It also feels like 
TreeMultimap.firstEntry(), as a name, would be kind of ambiguous between an 
Entry<K, Collection<V>>, or an Entry<K, V>.

Original comment by wasserman.louis on 3 Jul 2013 at 4:46

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08