Java 实现简化流API的框架,但使用Builder
有一项任务需要完成这门课。我不太明白如何在这里使用构建器模式?(如果这些是类的字段,那么,请理解,但是使用方法它不起作用,我不明白(有经验的家伙,请帮我一下) 有一些条件:Java 实现简化流API的框架,但使用Builder,java,builder,Java,Builder,有一项任务需要完成这门课。我不太明白如何在这里使用构建器模式?(如果这些是类的字段,那么,请理解,但是使用方法它不起作用,我不明白(有经验的家伙,请帮我一下) 有一些条件: 实现一个简化流API的框架 BestStream仅适用于整数类型 并且需要使用模式生成器 方法-初始数据 方法映射-将一个数字转换为另一个数字(元素*2) 方法过滤器-过滤元素流(元素==2) 方法收集-根据指定的映射和筛选条件从源中收集所有元素 import java.util.List; 导入java.util.fu
- 方法-初始数据
- 方法映射-将一个数字转换为另一个数字(元素*2)
- 方法过滤器-过滤元素流(元素==2)
- 方法收集-根据指定的映射和筛选条件从源中收集所有元素
import java.util.List;
导入java.util.function.function;
导入java.util.function.Predicate;
公共类最佳流{
公共静态最佳流(列表源){
抛出新的UnsupportedOperationException();
}
公共最佳流地图(功能乐趣){
抛出新的UnsupportedOperationException();
}
公共最佳流过滤器(谓词乐趣){
抛出新的UnsupportedOperationException();
}
公共列表收集(){
抛出新的UnsupportedOperationException();
}
}
我认为您案例中的字段是“函数函数”(函数和谓词),您可以注意到它们作为参数传递给您的方法
在此视图下,您可以应用生成器,在其中可以“构建”按以下顺序运行的流:
private static class BestStream {
private final Stream<Integer> source;
private Function<Integer, Integer> map;
private Predicate<Integer> filter;
private BestStream(Stream<Integer> source) {
this.source = source;
}
public static BestStream of(List<Integer> source) {
return new BestStream(source.stream());
}
public static void main(String[] args) {
final List<Integer> collect =
BestStream.of(Arrays.asList(1, 2, 3, 4, 2, 2, 9))
.filter(i -> Objects.equals(i, 2))
.map(i -> i * 2)
.collect();
}
public BestStream map(Function<Integer, Integer> fun) {
this.map = fun;
return this;
}
public BestStream filter(Predicate<Integer> fun) {
this.filter = fun;
return this;
}
public List<Integer> collect() {
return source.map(map).filter(filter).collect(Collectors.toList());
}
}
私有静态类BestStream{
私有最终流源;
私有功能图;
私有谓词过滤器;
私有最佳流(流源){
this.source=源;
}
公共静态最佳流(列表源){
返回新的最佳流(source.stream());
}
公共静态void main(字符串[]args){
最终名单收集=
BestStream.of(数组.asList(1,2,3,4,2,2,9))
.filter(i->Objects.equals(i,2))
.map(i->i*2)
.收集();
}
公共最佳流地图(功能乐趣){
this.map=乐趣;
归还这个;
}
公共最佳流过滤器(谓词乐趣){
这个过滤器=有趣;
归还这个;
}
公共列表收集(){
返回source.map(map.filter(filter.collect)(Collectors.toList());
}
}
我认为解决这个问题的最简单方法是将其分解为多个类
首先,API的接口用户可以看到:
包示例;
导入java.util.List;
导入java.util.function.IntPredicate;
导入java.util.function.IntUnaryOperator;
公共接口最佳流{
静态最佳流(列表){
返回新的BestStreamListSource(列表);
}
最佳流过滤器(IntPredicate谓词);
最佳流图(IntUnaryOperator操作员);
List collect();
}
第二,内部接口:
包示例;
公共接口BestStreamSource扩展了BestStream{
整数next();
}
现在是内部BestStreamSource
接口的基本实现,该接口稍后由实际实现使用:
包示例;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.function.IntPredicate;
导入java.util.function.IntUnaryOperator;
公共抽象类BaseBestStreamSource实现了BestStreamSource{
@凌驾
公共最佳流筛选器(IntPredicate谓词){
返回新的BestStreamFilteredSource(此,谓词);
}
@凌驾
公共最佳流图(IntUnaryOperator操作员){
返回新的BestStreamMappingSource(此,运算符);
}
@凌驾
公共列表收集(){
最终列表结果=新建ArrayList();
整数值=null;
而((value=next())!=null){
结果:增加(价值);
}
返回结果;
}
}
现在,您有3个类实际完成了这项工作:
- 一个源,从列表中读取元素
- 根据任何来源进行过滤的人
- 基于任何源进行映射的人
包示例;
导入java.util.List;
导入java.util.Objects;
公共类BestStreamListSource扩展了BaseBestStreamSource{
私人最终名单;
私有整数索引;
公共最佳流列表源(列表){
this.list=Objects.requirennull(列表);
该指数=0;
}
@凌驾
公共整数next(){
if(this.index
包示例;
导入java.util.Objects;
导入java.util.function.IntPredicate;
公共类BestStreamFilteredSource扩展了BaseBestStreamSource{
私人最终来源母公司;
私有定语谓语;
公共BestStreamFilteredSource(BestStreamSource父级,IntPredicate谓词){
this.parent=Objects.requirennull(父级);
this.predicate=Objects.requirennull(谓词);
}
@凌驾
公共整数next(){
最终整数[]持有人=新整数[1];
整数值=null;
布尔foundMatch=false;
而(!foundMatch&&(value=this.parent.next())!=null){
foundMatch=this.predicate.test(值);
}
if(foundMatch){
返回值;
}否则{
返回null;
}
}
}
包示例;
导入java.util.Objects;
导入java.util.function.IntUnaryOperator;
公共类BestStreamMappingSource扩展了BaseBestStreamSource{
私人财务
private static class BestStream {
private final Stream<Integer> source;
private Function<Integer, Integer> map;
private Predicate<Integer> filter;
private BestStream(Stream<Integer> source) {
this.source = source;
}
public static BestStream of(List<Integer> source) {
return new BestStream(source.stream());
}
public static void main(String[] args) {
final List<Integer> collect =
BestStream.of(Arrays.asList(1, 2, 3, 4, 2, 2, 9))
.filter(i -> Objects.equals(i, 2))
.map(i -> i * 2)
.collect();
}
public BestStream map(Function<Integer, Integer> fun) {
this.map = fun;
return this;
}
public BestStream filter(Predicate<Integer> fun) {
this.filter = fun;
return this;
}
public List<Integer> collect() {
return source.map(map).filter(filter).collect(Collectors.toList());
}
}