Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stream objects of incompatible types into a list?

My question is: Given a list of persons, return all students.

Here are my classes:

Person class

public class Person {
}

Student class

public class Student extends Person {
}

Method

public static List<Student> findStudents(List<Person> list) {


    return list.stream()
            .filter(person -> person instanceof Student)
            .collect(Collectors.toList());
}

I'm getting a compile error: incompatible types: inference variable T has incompatible bounds

How do I return all the students from the list using stream without getting this error.

like image 701
jennifer lawrence Avatar asked Sep 25 '19 08:09

jennifer lawrence


People also ask

How to handle incompatible types error in Java?

Swapping the inappropriate constructor call with an instance of the correct type solves the issue, as shown in Fig. 6(b). Fig. 6 also serves as an example to show how the incompatible types error is, in fact, a generalization of the method X in class Y cannot be applied to given types error explored in [4].

Can we convert stream to list in Java?

Stream class has toArray() method to convert Stream to Array, but there is no similar method to convert Stream to List or Set. Java has a design philosophy of providing conversion methods between new and old API classes e.g. when they introduced Path class in JDK 7, which is similar to java.

Do streams modify original list?

That means list. stream(). filter(i -> i >= 3); does not change original list. All stream operations are non-interfering (none of them modify the data source), as long as the parameters that you give to them are non-interfering too.

What is list to stream in Java?

A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. Below are various methods to convert List to Stream in Java:

How to convert an infinite stream into a list in Java?

To convert an infinite stream into list, we must limit the stream to a finite number of elements. Given example will work in case of stream of primitives. Program output. 5. Conclusion Clearly, We can use Collectors.toList () function is variety of ways to collect stream elements into an list in all usescases.

Why do I get incompatible types error?

Sometimes, the incompatible types error can occur due to basic negligence, where the only mistake is an accidental mis-declaration of a variable’s type (Fig. 3 (a)). In these instances, the issue is quite obvious and simply correcting the type declaration solves the problem (Fig. 3 (b)).

What does incompatible mean in C++?

Incompatible, in this context, means that the source type is both different from and unconvertible (by means of automatic type casting) to the declared type. This might seem like a syntax error, but it is a logical error discovered in the semantic phase of compilation.


1 Answers

return list.stream()
           .filter(Student.class::isInstance)
           .map(Student.class::cast)
           .collect(Collectors.toList());

It should be a cast there, otherwise, it's still a Stream<Person>. The instanceof check doesn't perform any cast.

Student.class::isInstance and Student.class::cast is just a preference of mine, you could go with p -> p instanceof Student and p -> (Student)p respectively.

like image 70
Andrew Tobilko Avatar answered Sep 19 '22 02:09

Andrew Tobilko