Converting between list types, java

So, I am writing a method that returns List<V>

. However, the list I made in the method is List<Vertex<V>>

. I was wondering if there was a way to convert List<Vertex<V>>

to List<V>

to match the return type other than just removing the "Vertex" part. Thank!

+3


source to share


2 answers


If you are using java 8, then an easy way to map a collection to another type of collection is just to write:

list.stream().map(vertex -> vertex.get()).collect(Collectors.toList());     

      



vertex.get()

there should be any code that accepts Vertex<V>

and converts it to V

.

+3


source


The short answer is no, you cannot solve it with "casting" alone. V

is not the same as Vertex<V>

, so you need to extract an object V

from each of your objects Vertex<V>

.



+1


source







All Articles