简体   繁体   中英

How to get Value of object from list of objects by Using Stream API

From the below code require to get values based on type from List,

List<Type> types= Arrays.asList(new Type("type1","Values1"),new Type("type2","Values2"));

List<AnotherType> anotherTypes = new ArrayList<>();

for (Type type:types){
    AnotherType  anotherType = new AnotherType();
    
    anotherType.settype1Value(?);
    anotherType.settype2Value(?);

    anotherTypes.add(anotherType);
}

easy way to find values in the?.

I am not sure I understand you question. Next time you ask a question on StackOverflow try to provide the classes you are using and try to explain what exatly are you trying to achieve.

Having said that, to transform one object into another, using streams, you can use map() . Something like that:

List<AnotherType> anotherType = types
    .stream()
    .map(t -> new AnotherType(t.getTypeValue1(), t.getTypeValue2()))
    .collectors(Collectors.toList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM