leadingSkill
leadingSkill

Reputation: 377

How to define a method in an interface that can return a list of any type without throwing unchecked assignment warnings?

I have an interface like this:

public interface Data<T> {
    List<T> getData();
}

Implementation of the interface:

public class HealthData implements Data {

    public List<HealthDataEntry> getData() {

            // Do something
    }
}

When I invoke the method I get an unchecked assignment warning:

Data data = new HealthData();
List<HealthDataEntry> healthData = data.getData()   //Unchecked assignment warning thrown here

It works but the warning is annoying, can you please advice me on how to fix this?

Upvotes: 0

Views: 47

Answers (1)

Andy Turner
Andy Turner

Reputation: 140318

public class HealthData implements Data<HealthDataEntry>

And

Data<HealthDataEntry> data = new HealthData();

Otherwise you are using a raw type.

Upvotes: 5

Related Questions