Fun with method references

Paul Benedict pbenedict at apache.org
Wed Aug 4 14:45:02 PDT 2010


Do you think JDK 7 will come with new utility SAM interfaces? I am sure
there are other common patterns throughout Java that could be SAM-ified.

On Wed, Aug 4, 2010 at 4:33 PM, Brian Goetz <brian.goetz at oracle.com> wrote:

> Check this out -- this works with the latest prototype:
>
> interface Extractor<T, U> {
>     public U get(T t);
> }
>
> class Arrays {
>     public <T, U extends Comparable<? super U>>
>     void sortBy(T[] array, Extractor<T, U> extractor) { ... }
> }
>
>
> String[] strings = ...
> Arrays.sortBy(strings, String#length);  // sort strings by length
>
> Foo[] foos = ...
> Arrays.sortBy(foos, Foo#getA);          // sort by JavaBean property A
>
>
>
>


More information about the lambda-dev mailing list