rajrembo / google-collections

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

Maps.transformValues for SortedMap #291

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hallo, I'd have a request for enhancement:

I need a live view of SortedMap. Unfortunately, the method
<K,V1,V2> Map<K,V2> Maps.transformValues(Map<K,V1> fromMap, Function<?
super V1,V2> function)
accepts SortedMap though, however, the returned Map "looses" the SortedMap
interface.

public class Exp {

    public static void main(String[] args) {
        SortedMap<Integer,String> values = new TreeMap<Integer,String>();
        values.put(1, "one");
        values.put(2, "two");
        values.put(3, "three");
        values.put(4, "four");
        values.put(5, "five");

        // works but is not a SortedMap:
        final Map<Integer,String> view = Maps.transformValues(values, new
Function<String,String>() {
            public String apply(String from) {
                return from.toUpperCase();
            }
        });
        System.out.println(view); 

        // does not work (CCE):
        final SortedMap<Integer,String> viewSorted =
(SortedMap<Integer,String>)Maps.transformValues(values,...

        // this would be great (either):
        final SortedMap<Integer,String> viewSorted = Maps.transformValues(values,...
        final SortedMap<Integer,String> viewSorted =
SortedMaps.transformValues(values,...

        System.out.println(viewSorted.firstKey());
    }
}

Thanks!

Original issue reported on code.google.com by tomas.za...@gmail.com on 6 Nov 2009 at 9:12

GoogleCodeExporter commented 8 years ago
Hmm... considering this method plus the three filtering ones, that makes four 
methods
we might have to duplicate.  I feel ambivalent about that.

An alternative is to create a SortedMaps class to house only the things special 
to
SortedMaps -- these four and maybe a scant few other things.

Another possibility -- I don't know if it's a good one -- would be for us to 
specify
that if you pass a SortedMap to one of the existing methods, you can safely 
downcast
the result back to SortedMap.  (and then, you know, do the implementation like 
that too.)

Original comment by kevin...@gmail.com on 6 Nov 2009 at 10:00

GoogleCodeExporter commented 8 years ago
In addition, a Sets.filtered() method could handle SortedSets.

I'd rather not force people to cast, since casts are a code smell that should be
avoided when feasible.

Original comment by jared.l....@gmail.com on 6 Nov 2009 at 6:04

GoogleCodeExporter commented 8 years ago
I think you're totally right about that.

Original comment by kevin...@gmail.com on 6 Nov 2009 at 6:13

GoogleCodeExporter commented 8 years ago
My +1 to SortedMaps.transformValues rather than downcasting.

In addition, also this method would be IMHO useful:
<K,V1,V2> SortedMaps.transformEntries(
  SortedMap<K,V1> fromMap,
  Function<? super Map.Entry<K,V1>,V2> function
)
This is similar to transformValues but new values also depend on key which is
practical when one works with mathematical-function-like data.

Original comment by tomas.za...@gmail.com on 24 Nov 2009 at 1:15

GoogleCodeExporter commented 8 years ago
That transformEntries method is reasonable. 

Whoever's interested in it should file a separate issue, so it's not hidden 
within
issue.this 

Original comment by jared.l....@gmail.com on 28 Nov 2009 at 8:03

GoogleCodeExporter commented 8 years ago
This issue has been moved to the Guava project (keeping the same id number). 
Simply replace 'google-collections' with 'guava-libraries' in your address 
bar and it should take you there.

Original comment by kevinb@google.com on 5 Jan 2010 at 11:09