低价郑州网站建设,php做简单网站教程视频,网站 建设运行情况报告,网站建设飠金手指科杰十二jdk11换jdk8版本在JDK 8中#xff0c;我们终于可以使用流了#xff0c;除了您使用的API无法产生流的时代之外#xff0c;其他一切都很好。 然后#xff0c;您最终编写了一个包装器类方法#xff0c;该方法允许您将迭代器转换为Stream#xff0c;因为您错过了流。 public… jdk11换jdk8版本 在JDK 8中我们终于可以使用流了除了您使用的API无法产生流的时代之外其他一切都很好。 然后您最终编写了一个包装器类方法该方法允许您将迭代器转换为Stream因为您错过了流。 public static T StreamT asStream(IteratorT it) {return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it,Spliterator.IMMUTABLE | Spliterator.ORDERED),false);
} 现在有一些方法可以在迭代和生成的情况下以编程方式生成流但是这两种方法都会生成无限流而在大多数情况下您确实想将现有接口改编为有限流。 在JDK 9中通过引入一种新形式的迭代方法很好地解决了该问题该方法允许您提供一个谓词来表示流的结束。 在下面的示例中我将使用谓词该谓词将一直持续到您获得流的空条目为止我将留给读者以提供更多富于想象力的谓词用法。 在这个简单的示例中我使用Throwable的getCause方法来使我们沿着错误的链接列表移动。 请注意与预发布版本相比这将花费很少的代码。 // Simple linked list
//
Exception e new Exception(one);
Exception e2 new Exception(two,e);
Exception e3 new Exception(three, e2);Stream.iterate(e3, Objects::nonNull, Throwable::getCause)// Output the messages in turn.map(Throwable::getMessage).forEach(System.out::println); 第二个示例将ReferenceQueue转换为Stream以便我们可以轻松地耗尽其内容以根据需要进行处理。 这段代码有些不同因为容器与要处理的对象不同因此我们使用相同的方法提供种子和下一个值。当队列为空时此方法返回null。 ReferenceQueueThing queue new ReferenceQueue();// Make some things and then collect them
WeakReference one new WeakReferenceThing(new Thing(), queue);
WeakReference two new WeakReferenceThing(new Thing(), queue);
System.gc(); System.gc(); System.gc(); System.gc(); System.gc();Stream.Reference? extends Thingiterate(queue.poll(), Objects::nonNull, v - queue.poll()).forEach(System.out::println); 第三个示例显示了在Node树上的遍历请注意当我们工作到叶子的末尾时嵌套的流迭代器将备份列表。 Node root doc.getDocumentElement();Stream.iterate(root,Objects::nonNull,v - {if (v.getFirstChild()!null) {return v.getFirstChild();}if (v.getNextSibling()!null) {return v.getNextSibling();}return Stream.iterate(v, Objects::nonNull, Node::getParentNode).filter(node - node.getNextSibling()!null).map(Node::getNextSibling).findFirst().orElse(null);}).map(Node::getNodeName).forEach(System.out::println); 因此通过进行少量的心理操练就可以将大多数旧版API转换为干净的Stream从而可以忽略那些讨厌的老式循环。 而且如果您陷于JDK 8中那么很容易使用之前的asStream来组合类似的功能 public staticT StreamT iterateFinite(T seed, Predicate? super T hasNext, UnaryOperatorT next) {return asStream(new Iterator() {T current seed;Overridepublic boolean hasNext() {return hasNext.test(current);}Overridepublic T next() {if (current null) {throw new NoSuchElementException();}try {return current;} finally {current next.apply(current);}}});
}翻译自: https://www.javacodegeeks.com/2018/12/jdk-9-everything-can-stream.htmljdk11换jdk8版本