首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Stream<CustomType>创建Stream<String>?

使用Stream<CustomType>创建Stream<String>?
EN

Stack Overflow用户
提问于 2021-11-05 17:04:26
回答 2查看 77关注 0票数 0

假设我有一个Stream<String>,用什么最简洁的方法可以获得一个Stream<CustomType>,其中Stream<CustomType>的每个元素都将Stream<String>的每个元素作为其成员变量之一的值:

例如:

代码语言:javascript
运行
复制
class CustomType {
    private String id;

    public void setId(String id) {
        this.id = id;
    } 
}

class Test {
    public static void main (String... args) {
        Stream<String> source = Stream.of("A", "B", "C"); 
        
        Stream<CustomType> result = source.map(s -> {
            CustomType obj = new CustomType();
            obj.setId(s);
            return obj;
        });  
    }
}

有办法缩短这个时间吗?

后来编辑了:寻找一种方法而不是重写CustomType的构造函数,并避免编写手动创建CustomType对象的模板,例如,让Stream为我创建CustomType对象,这样我就可以指定String应该设置的是什么setter (例如:setID())。

EN

回答 2

Stack Overflow用户

发布于 2021-11-05 17:06:42

如果可以更改自定义类型的构造函数,则可以:

代码语言:javascript
运行
复制
class CustomType {
    private String id;

    public CustomType(String id) {
      this.id = id;
    }

    public void setId(String id) {
        this.id = id;
    } 
}

class Test {
    public static void main (String... args) {
        Stream<String> source = Stream.of("A", "B", "C");         
        Stream<CustomType> result = source.map(CustomType::new);  
    }
}

如果不能添加新的构造函数,那么最好的方法是将设置逻辑移出流处理器之外,如下所示:

代码语言:javascript
运行
复制
class Test {
    public static void main (String... args) {
        Stream<String> source = Stream.of("A", "B", "C");         
        Stream<CustomType> result = source.map(Test::customTypeWithId);  
    }

    private static CustomType customTypeWithId(String id) {
        CustomType obj = new CustomType();
        obj.setId(s);
        return obj;
    }
}
票数 4
EN

Stack Overflow用户

发布于 2021-11-05 17:52:49

对于任何CustomType,您都可以这样做。

  • 定义一个函数来创建实例并设置值;
  • 然后用源流和该函数调用方法以返回新的Stream

代码语言:javascript
运行
复制
Stream<String> source = Stream.of("A", "B", "C");

Function<String,CustomType> fnc = (str) -> {
        CustomType t = new CustomType();
        t.setId(str);
        return t;};

Stream<CustomType> stream = customStream(source, fnc);
    
public static <T> Stream<T> customStream(Stream<String> source,
        Function<String, T> fnc) {
    Builder<T> builder = Stream.builder();
    source.forEach(k -> builder.add(fnc.apply(k)));
    return builder.build();
}

当然,如果您的自定义类型实现了一个以T setId(String s)作为方法的接口,则会更好。这方面的一个例子是:

代码语言:javascript
运行
复制
interface SetIDInterface<T> {
    public T setId(String s);
}

class CustomType implements SetIDInterface<CustomType> {
    public String id;
    
    public CustomType setId(String id) {
        this.id = id;
        return this;   
    }
}

Stream<String> source = Stream.of("A", "B", "C");
Stream<CustomType> stream = customStream(source, CustomType::new);

    
public static <T extends SetIDInterface<T>> Stream<T> 
               customStream(Stream<String> source, Supplier<T> sup) {
    Builder<T> builder = Stream.builder();
    source.forEach(k -> builder.add(sup.get().setId(k)));
    return builder.build();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69856813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档