How to convert Map to List of User Object?

bpereira :

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;
}
GBlodgett :

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());   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=73028&siteId=1