Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA: ImmutableSet as List

I currently get returned an ImmutableSet from a function call (getFeatures()) and due to the structure of the rest of my code to be executed later on- it would be much easier to change this to a List. I have tried to cast it which produces a runtime exception. I have also looked around for a function call to convert it to a list to no avail. Is there a way to do this? My most recent [failed] attempt is shown below:

ImmutableSet<FeatureWrapper> wrappersSet =  getFeatures();
List<FeatureWrapper> wrappers = (List<FeatureWrapper>) wrappersSet;

I have found wrapperSet.asList() which will give me an ImmutableList however i would much rather prefer a mutable list

like image 563
GregH Avatar asked Dec 06 '22 21:12

GregH


1 Answers

You can't cast a Set<T> into a List<T>. They are entirely-different objects. Just use this copy constructor which creates a new list out of a collection:

List<FeatureWrapper> wrappers = new ArrayList<>(wrappersSet);
like image 70
Vivin Paliath Avatar answered Dec 08 '22 09:12

Vivin Paliath