简体   繁体   English

如何使用 Stream API 从对象列表中获取 object 的值

[英]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.下次您在 StackOverflow 上提出问题时,请尝试提供您正在使用的类,并尝试解释您想要实现的目标。

Having said that, to transform one object into another, using streams, you can use map() .话虽如此,要将一个 object 转换为另一个,使用流,您可以使用map() Something like that:像这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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