Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Map to List of User Object?

I have a HashMap with the following structure:

Map<Object, List<OtherObject>>

And I want to convert it to:

List<FinalObject>

being FinalObject:

public class FinalObject {
    private Object object;
    private List<OtherObject> otherObject;
}
like image 753
bpereira Avatar asked Jan 02 '23 04:01

bpereira


1 Answers

Assuming you have a constructor like:

public FinalObject(Object obj, List<OtherObject> list) {
    this.object = obj;
    this.otherObject = list;
}

then you can do:

List<FinalObject> newList = map.entrySet()
                               .stream().map(e -> new FinalObject(e.getKey(), e.getValue()))
                               .collect(Collectors.toList());   
like image 197
GBlodgett Avatar answered Jan 13 '23 13:01

GBlodgett