繁体   English   中英

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

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

从下面的代码需要从列表中获取基于类型的值,

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

在?中查找值的简单方法。

我不确定我是否理解你的问题。 下次您在 StackOverflow 上提出问题时,请尝试提供您正在使用的类,并尝试解释您想要实现的目标。

话虽如此,要将一个 object 转换为另一个,使用流,您可以使用map() 像这样的东西:

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