查看: 1527| 回复: 1
跳转到指定楼层
上一主题 下一主题
收起左侧

请问如何自己实现Java8的Stream?

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
就是要实现基本的of(),
concat(),
map(),
filter()等方法,实在没有思路,求执教

上一篇:Queue Reconstruction by Height这道题死活看不懂
下一篇:今年的SIGMOD Programming Contest 2018
🔗
magicsets 2018-3-19 10:43:42 | 只看该作者
全局:
本帖最后由 magicsets 于 2018-3-19 10:54 编辑

我写了一份简单代码供参考~

不过如果要优化性能的话,实现上会复杂很多:
(1) 需要特化(specialize)基础类型(例如int, double)的实现,参照Java的IntStream、DoubleStream
(2) 需要考虑lazy evaluation,以减少物化(materialize)数据造成的对内存带宽的冲击和对cache locality的破坏
(3) 如果要做lazy evaluation,实际上就是要生成中间的query plan,那么可以进行进一步的query optimization,比如filter pushdown
(4) 需要考虑对多核并行执行的支持
(5) 再加上对分布式的支持,你就得到了类似Spark的东西..
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collection;
  4. import java.util.Iterator;

  5. public class Main {
  6.   public static void main(String[] args) {
  7.     int[] first = new int[] { 0, 1, 2 };
  8.     int[] second = new int[] { 4, 5, 6 };

  9.     MyStream.concat(MyStream.of(first).map(x -> (int)x + 1),
  10.                     MyStream.of(second))                        // 1, 2, 3, 4, 5, 6
  11.             .map(x -> (int)x * (int)x)                          // 1, 4, 9, 16, 25, 36
  12.             .filter(x -> (int)x > 5 && (int)x < 30)             // 9, 16, 25
  13.             .reduce(0, (x, y) -> (int)x + (int)y)               // 50
  14.             .each(x -> System.out.println("sum = " + x));       // sum = 50
  15.   }
  16. }

  17. // map()方法的参数类型:一元函数
  18. interface UnaryOperation {
  19.   public Object apply(Object operand);
  20. }

  21. // reduce()方法的参数类型:二元函数
  22. interface BinaryOperation {
  23.   public Object apply(Object lhs, Object rhs);
  24. }

  25. // filter()方法的参数类型:一元谓词
  26. interface UnaryPredicate {
  27.   public boolean apply(Object operand);
  28. }

  29. // each()方法的参数类型
  30. interface Consumer {
  31.   public void apply(Object operand);
  32. }

  33. // 不考虑性能的简单实现
  34. @SuppressWarnings("rawtypes")
  35. abstract class MyStream implements Iterable {
  36.   public MyStream filter(UnaryPredicate predicate) {
  37.     ArrayList<Object> results = new ArrayList<Object>();
  38.     for (Object o : this) {
  39.       if (predicate.apply(o)) {
  40.         results.add(o);
  41.       }
  42.     }
  43.     return of(results);
  44.   }

  45.   public MyStream map(UnaryOperation operation) {
  46.     ArrayList<Object> results = new ArrayList<Object>();
  47.     for (Object o : this) {
  48.       results.add(operation.apply(o));
  49.     }
  50.     return of(results);
  51.   }

  52.   public MyStream reduce(Object init, BinaryOperation operation) {
  53.     Object accumulated = init;
  54.     for (Object o : this) {
  55.       if (accumulated == null) {
  56.         accumulated = o;
  57.       } else {
  58.         accumulated = operation.apply(accumulated, o);
  59.       }
  60.     }
  61.     ArrayList<Object> results = new ArrayList<Object>();
  62.     if (accumulated != null) {
  63.       results.add(accumulated);
  64.     }
  65.     return of(results);
  66.   }

  67.   public MyStream reduce(BinaryOperation operation) {
  68.     return reduce(null, operation);
  69.   }

  70.   public void each(Consumer consumer) {
  71.     for (Object o : this) {
  72.       consumer.apply(o);
  73.     }
  74.   }


  75.   public static MyStream concat(MyStream a, MyStream b) {
  76.     ArrayList<Object> results = new ArrayList<Object>();
  77.     for (Object o : a) {
  78.       results.add(o);
  79.     }
  80.     for (Object o : b) {
  81.       results.add(o);
  82.     }
  83.     return of(results);
  84.   }

  85.   public static MyStream of(int[] data) {
  86.     ArrayList<Integer> results = new ArrayList<Integer>();
  87.     for (int i = 0; i < data.length; ++i) {
  88.       results.add(data[i]);
  89.     }
  90.     return of(results);
  91.   }

  92.   public static MyStream of(Object[] data) {
  93.     return new ArrayStream(data);
  94.   }

  95.   public static MyStream of(Collection data) {
  96.     return new CollectionStream(data);
  97.   }
  98. }

  99. class ArrayStream extends MyStream {
  100.   private Object[] data;

  101.   public ArrayStream(Object[] data) {
  102.     this.data = data;
  103.   }

  104.   @Override
  105.   public Iterator<Object> iterator() {
  106.     return Arrays.asList(data).iterator();
  107.   }
  108. }

  109. @SuppressWarnings("rawtypes")
  110. class CollectionStream extends MyStream {
  111.   private Collection data;

  112.   CollectionStream(Collection data) {
  113.     this.data = data;
  114.   }

  115.   @Override
  116.   public Iterator iterator() {
  117.     return data.iterator();
  118.   }
  119. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表