Java 业务链

Java 业务链,java,java-stream,Java,Java Stream,我有一个接口,它接受一个字符串并返回一个转换后的字符串 我有一些类将以不同的方式进行转换。 Java中有没有办法创建这些类的流并对字符串进行转换 例如: class MyClass implements MyOperation { String execute(String s) { return doSomething(s); } } class MyClass2 implements MyOperation { String execute(String s) { return

我有一个接口,它接受一个字符串并返回一个转换后的字符串

我有一些类将以不同的方式进行转换。 Java中有没有办法创建这些类的流并对字符串进行转换

例如:

class MyClass implements MyOperation {
   String execute(String s) { return doSomething(s); }
}

class MyClass2 implements MyOperation {
   String execute(String s) { return doSomething(s); }
}

ArrayList<MyClass> operations = new ArrayList<>();

operations.add(new MyClass());
operations.add(new MyClass2());
...

operations.stream()...
类MyClass实现MyOperation{
字符串执行(字符串s){返回doSomething(s);}
}
类MyClass2实现MyOperation{
字符串执行(字符串s){返回doSomething(s);}
}
ArrayList操作=新建ArrayList();
添加(新的MyClass());
添加(新的MyClass2());
...
operations.stream()。。。
为了对一个字符串进行大量转换,我可以制作一个流吗?我考虑过
.reduce()
,但它对数据类型要求严格。

数组列表应该是
ArrayList
,否则调用
operations.add(new MyClass2())将产生编译错误

也就是说,您正在寻找:

  • “myString”
    是标识值

  • (x,y)->y.execute(x)
    是要应用的累加器函数

  • (a,b)->{…
    是仅在流并行时使用的组合器函数。因此,对于顺序流,您无需担心它

    你可能还想看看我不久前发布的答案

数组列表应为
ArrayList
,否则调用
operations.add(new MyClass2());
将产生编译错误

也就是说,您正在寻找:

  • “myString”
    是标识值

  • (x,y)->y.execute(x)
    是要应用的累加器函数

  • (a,b)->{…
    是仅在流并行时使用的组合器函数。因此,对于顺序流,您无需担心它

    你可能还想看看我不久前发布的答案


您的类都实现了将
字符串
转换为
字符串
的方法。换句话说,它们可以由
函数
表示。它们可以按如下方式组合并应用于单个字符串:

List<Function<String,String>> ops = new ArrayList<> ();
ops.add (s -> s + "0"); // these lambda expressions can be replaced with your methods:
                        // for example - ops.add((new MyClass())::execute);
ops.add (s -> "1" + s);
ops.add (s -> s + " 2");
// here we combine them
Function<String,String> combined = 
    ops.stream ()
       .reduce (Function.identity(), Function::andThen);
// and here we apply them all on a String
System.out.println (combined.apply ("dididi"));

您的类都实现了将
字符串
转换为
字符串
的方法。换句话说,它们可以由
函数
表示。它们可以按如下方式组合并应用于单个字符串:

List<Function<String,String>> ops = new ArrayList<> ();
ops.add (s -> s + "0"); // these lambda expressions can be replaced with your methods:
                        // for example - ops.add((new MyClass())::execute);
ops.add (s -> "1" + s);
ops.add (s -> s + " 2");
// here we combine them
Function<String,String> combined = 
    ops.stream ()
       .reduce (Function.identity(), Function::andThen);
// and here we apply them all on a String
System.out.println (combined.apply ("dididi"));

你想模仿Java方法,首先,我觉得奇怪的是,你将操作声明为MyClass的arraylist,然后继续向其中添加MyClass2。也许你想让它成为MyOperations的arraylist?其次,是的,你可以创建一个包装类来存储一个字符串,让流按顺序排列(如果您的操作没有转换,否则并行就可以了),并使用foreach将字符串替换为操作结果,但这并不是真正应该使用流的目的。在这里迭代操作似乎更合适。
ArrayList
应该是
ArrayList
否则调用
operations.add(新建MyClass2());
将产生编译错误。不过,您可能正在查找reduce-->
String result=operations.stream().reduce(“myString”,(x,y)->y.doSomething(x),(a,b)->{抛出新的运行时异常(“未实现”);})
您想模仿Java方法,首先,我觉得奇怪的是,您将操作声明为MyClass的arraylist,然后继续向其中添加MyClass2。也许您想让它成为MyOperations的arraylist?其次,是的,您可以创建一个包装类来存储字符串,使流具有顺序(如果您的操作没有转换,否则并行就可以了),并使用foreach将字符串替换为操作结果,但这并不是真正应该使用流的目的。在这里迭代操作似乎更合适。
ArrayList
应该是
ArrayList
否则调用
operations.add(新建MyClass2());
将产生编译错误。不过,您可能正在查找reduce-->
String result=operations.stream().reduce(“myString”,(x,y)->y.doSomething(x),(a,b)->{抛出新的运行时异常(“未实现”);})
感谢您解释合并器函数。编译器给出了令人困惑的消息。@JackFlamp非常欢迎您。这些消息是什么意思?我只是想用
a.equals(b)
替换您抛出的runtimeexception,看看合并器是什么,execute方法是
方法execute(String)在类型General.MyOperation不适用于参数(U)
。reduce表示
方法reduce(U,双函数{},(a,b)->{})
顺便说一句,我将您的
y.doSomething(x)
更改为
y.execute(x)
,这看起来更准确、更可靠worked@JackFlamp您可以使用组合器-->
(a,b)->{System.out.println(“正在调用它吗?”);返回“dummy”;}
感谢您解释合并器函数。编译器给出了令人困惑的消息。@JackFlamp非常欢迎您。这些消息是什么意思?我只是想用
a.equals(b)
替换您抛出的runtimeexception,看看合并器是什么,execute方法是
方法execute(String)在类型General.MyOperation不适用于参数(U)
。reduce表示
方法reduce(U,双函数{},(a,b)->{})
顺便说一句,我将您的
y.doSomething(x)
更改为
y.execute(x)
,这看起来更准确、更可靠worked@JackFlamp你可以测试
1dididi0 2