How to convert a list of class to a list of interface? In stockQuote method

Im fetching data from api and storing it into a tiingocandle list but i need to return a list of candle how should i convert it into a list of candle.

These are some of the references i could think of could be important for you to get a list of candles. Please let me know if it was helpful or not.

no actually , i want to convert a list of class to a list of interface, not an array to a list

I am getting data to a list of tiingocandle but the return type of function is list of candle so i want ot convert it to a list of candle

I think what Pallav meant to say that getStockQuote() method in ,its return type in a list of which is an interface and would be of such more sense if it would have returned a list of class’s object which implements that abstract.

Well i have the same doubt too.

You can return the list as it is, since Tiingo Candle implements the Candle interface. Therefore, it’ll be resolved at run time and your method would work fine.

but it is showing an error on returning a list stating that unable to convert tiingocandle to candle object

What you can do is that you can map the response with TiingoCandle[].class but store the results generated by object mapper in List< Candle. >
This soultion definitely works

What I meant is that you can deserialise with TiingoCandle when you use object mapper, but the answer can be stored in List of Candle type itself. You can call all functions of TiingoCandle from it as well. Try doing that and lemme know it it helps!

I tried it but it didn’t work

Actually it works.
I have done the same

It works fine. You’ll use it in further modules as well when another API service is added.

1 Like

may be im using wrong syntax, i’ll look for it

Ya That could be a possibility

I used a different one
I used TiingoCandle[].class
After than I used Array.asList(Your mapper outupt) to get the output as List< Candle >

does anyone know how can i close the ticket?

Hey @yakshit , I tried this but it not letting me use asList method

What issue are you facing?

I am storing the the response of api in tiingocandle array and the trying to use asList method but it says it is undefined for type Tiingocandle

U need to store the response in List < Candle >

1 Like