Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting between List types, java

Tags:

java

list

So I am writing a method that returns List<V>. However, the list that I've made in the method is List<Vertex<V>>. I was wondering if there's some way to convert List<Vertex<V>> to List<V> to match the return type other than just removing the "Vertex" part. Thanks!


1 Answers

If you are using java 8 there's an easy way to map a collection to a different type of collections simple write:

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

vertex.get() should be any code that takes a Vertex<V> and converts it to V.

like image 107
Stav Saad Avatar answered Mar 07 '26 06:03

Stav Saad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!