Not matching type of LIst<> and its element - Java

Viewed 63

I have a list of types DataRespose. I am using it to get a response from an external API and then to test it.

I am trying to create a new list with the same type and add a new element to it but I don't know-how.

I tried something like this:

List<DataResponse> dataResponseList = new ArrayList<>();
dataResponseList.add("dataId");

But I am getting error of:

Required type: DataResponse Provided: String`

I am aware that type is no the same, but then what should I add to be type DataResponse

DataResponse.java

public class DataResponse {

    public final String dataId;

    public DataResponse(String dataId) {
        this.dataId = dataId;
    }
}
2 Answers
List<DataResponse> dataResponseList = new ArrayList<>();
dataResponseList.add(new DataResponse("dataId"));

The list expects an object of DataResponse type.

  List<DataResponse> dataResponseList = new ArrayList<>();
    dataResponseList.add(new DataResponse("data"));
Related