incompatible types compile error
Boaz Nahum
boaznahum at gmail.com
Mon Nov 19 23:38:02 PST 2012
But it still fails (b64) even without the comparator
-------------------------------------------------
//http://cr.openjdk.java.net/~briangoetz/lambda/sotc3.html
List<Shape> blue = shapes.stream()
.filter(s -> s.getColor() == Color.BLUE)
.into(new ArrayList<>());
-------------------------------------------------------------------------------
java: incompatible types: java.util.ArrayList<java.lang.Object> cannot be
converted to java.util.List<soc.Shape>
Is it valid code ? Or should I add the type parameter ?:
-------------------------------------------------------
List<Shape> blue = shapes.stream()
.filter(s -> s.getColor() == Color.BLUE)
.into(new ArrayList<Shape>());
-------------------------------------------------------
Thanks
Boaz
On Mon, Nov 19, 2012 at 11:36 PM, Arul Dhesiaseelan <aruld at acm.org> wrote:
> Thanks Maurizio for the clarification.
>
> For me, the first usage fails to compile as expected. The second usage
> works perfectly fine, I wonder what is different that make second example
> work without the type parameter. I tested with today's build.
>
> List<Album> sortedFavs1 = albums.stream()
> .filter(a -> a.tracks.stream().anyMatch(t -> (t.rating >= 4)))
> .sorted(comparing((Function<Album, Comparable>) album ->
> album.name
> ))
> .into(new ArrayList<Album>());//java: incompatible types:
> java.util.stream.Stream.Destination cannot be converted to
> java.util.List<streams.Album>
>
> List<Album> sortedFavs2 = new ArrayList<Album>();
> albums.stream()
> .filter(a -> a.tracks.stream().anyMatch(t -> (t.rating >= 4)))
> .sorted(comparing((Function<Album, Comparable>) album ->
> album.name
> ))
> .into(sortedFavs2);
>
> Here is the working version with the type parameter.
>
> List<Album> sortedFavs = albums.stream()
> .filter(a -> a.tracks.stream().anyMatch(t -> (t.rating >= 4)))
> .sorted(comparing((Function<Album, String>) album -> album.name))
> .into(new ArrayList<Album>());
>
>
> - Arul
>
>
> On Mon, Nov 19, 2012 at 3:37 AM, Maurizio Cimadamore <
> maurizio.cimadamore at oracle.com> wrote:
>
> > The problem is that, since you are using Comparable (note the absence of
> > type-parameters) you are effectively triggering an uunchecked conversion
> > there - the result would be that the erased signature of 'into' will be
> > used instead - that is, the return type of into will be simply
> Destination
> > - not the type inferred from the argument - hence the incompatible types
> > error.
> >
> > Btw - I'm getting the error for both versions of the example you sent.
> >
> > Maurizio
> >
> >
> > On 16/11/12 23:01, Arul Dhesiaseelan wrote:
> >
> >> This works, btw.
> >>
> >> List<Album> sortedFavs = new ArrayList<Album>();
> >> albums.stream()
> >> .filter(a -> a.tracks.stream().anyMatch(t -> (t.rating >=
> >> 4)))
> >> .sorted(comparing((Mapper<**Comparable, Album>) album ->
> >> album.name))
> >> .into(sortedFavs);
> >>
> >>
> >>
> >> On Fri, Nov 16, 2012 at 12:38 PM, Remi Forax <forax at univ-mlv.fr> wrote:
> >>
> >> On 11/16/2012 10:57 PM, Arul Dhesiaseelan wrote:
> >>>
> >>>> Hi,
> >>>>
> >>>> I am trying to run a slightly modified version from the latest State
> of
> >>>>
> >>> the
> >>>
> >>>> Lambda libraries edition.
> >>>>
> >>>> List<Album> sortedFavs =
> >>>> albums.stream()
> >>>> .filter(a -> a.tracks.stream().anyMatch(t -> (t.rating
> >=
> >>>>
> >>> 4)))
> >>>
> >>>> .sorted(comparing((Mapper<**Comparable, Album>) album ->
> >>>> album.name))
> >>>> .into(new ArrayList<Album>());
> >>>>
> >>>> java: incompatible types: java.util.stream.Stream.**Destination cannot
> >>>> be
> >>>> converted to java.util.List<Album>
> >>>>
> >>>> Any idea what could be wrong here?
> >>>>
> >>> the signature of the method into.
> >>>
> >>> -Arul
> >>>>
> >>>> Rémi
> >>>
> >>>
> >>>
> >>>
> >
>
>
More information about the lambda-dev
mailing list