聊聊Elasticsearch的Iterables
为什么80%的码农都做不了架构师?>>>
序
本文主要研究一下Elasticsearch的Iterables
Iterables
elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/util/iterable/Iterables.java
public class Iterables {public static <T> Iterable<T> concat(Iterable<T>... inputs) {Objects.requireNonNull(inputs);return new ConcatenatedIterable<>(inputs);}static class ConcatenatedIterable<T> implements Iterable<T> {private final Iterable<T>[] inputs;ConcatenatedIterable(Iterable<T>[] inputs) {this.inputs = Arrays.copyOf(inputs, inputs.length);}@Overridepublic Iterator<T> iterator() {return Stream.of(inputs).map(it -> StreamSupport.stream(it.spliterator(), false)).reduce(Stream::concat).orElseGet(Stream::empty).iterator();}}/** Flattens the two level {@code Iterable} into a single {@code Iterable}. Note that this pre-caches the values from the outer {@code* Iterable}, but not the values from the inner one. */public static <T> Iterable<T> flatten(Iterable<? extends Iterable<T>> inputs) {Objects.requireNonNull(inputs);return new FlattenedIterables<>(inputs);}static class FlattenedIterables<T> implements Iterable<T> {private final Iterable<? extends Iterable<T>> inputs;FlattenedIterables(Iterable<? extends Iterable<T>> inputs) {List<Iterable<T>> list = new ArrayList<>();for (Iterable<T> iterable : inputs) {list.add(iterable);}this.inputs = list;}@Overridepublic Iterator<T> iterator() {return StreamSupport.stream(inputs.spliterator(), false).flatMap(s -> StreamSupport.stream(s.spliterator(), false)).iterator();}}public static <T> T get(Iterable<T> iterable, int position) {Objects.requireNonNull(iterable);if (position < 0) {throw new IllegalArgumentException("position >= 0");}if (iterable instanceof List) {List<T> list = (List<T>)iterable;if (position >= list.size()) {throw new IndexOutOfBoundsException(Integer.toString(position));}return list.get(position);} else {Iterator<T> it = iterable.iterator();for (int index = 0; index < position; index++) {if (!it.hasNext()) {throw new IndexOutOfBoundsException(Integer.toString(position));}it.next();}if (!it.hasNext()) {throw new IndexOutOfBoundsException(Integer.toString(position));}return it.next();}}
}
- Iterables提供了concat、flatten、get三个静态方法,其中concat返回的是ConcatenatedIterable;flatten返回的是FlattenedIterables;get方法会先判断是否是List类型是的话直接通过position取值返回,不是则遍历iterable获取指定index的元素
实例
elasticsearch-7.0.1/server/src/test/java/org/elasticsearch/common/util/iterable/IterablesTests.java
public class IterablesTests extends ESTestCase {public void testGetOverList() {test(Arrays.asList("a", "b", "c"));}public void testGetOverIterable() {Iterable<String> iterable = () ->new Iterator<String>() {private int position = 0;@Overridepublic boolean hasNext() {return position < 3;}@Overridepublic String next() {if (position < 3) {String s = position == 0 ? "a" : position == 1 ? "b" : "c";position++;return s;} else {throw new NoSuchElementException();}}};test(iterable);}public void testFlatten() {List<List<Integer>> list = new ArrayList<>();list.add(new ArrayList<>());Iterable<Integer> allInts = Iterables.flatten(list);int count = 0;for(@SuppressWarnings("unused") int x : allInts) {count++;}assertEquals(0, count);list.add(new ArrayList<>());list.get(1).add(0);// changes to the outer list are not seen since flatten pre-caches outer list on init:count = 0;for(@SuppressWarnings("unused") int x : allInts) {count++;}assertEquals(0, count);// but changes to the original inner lists are seen:list.get(0).add(0);for(@SuppressWarnings("unused") int x : allInts) {count++;}assertEquals(1, count);}private void test(Iterable<String> iterable) {try {Iterables.get(iterable, -1);fail("expected IllegalArgumentException");} catch (IllegalArgumentException e) {assertThat(e, hasToString("java.lang.IllegalArgumentException: position >= 0"));}assertEquals("a", Iterables.get(iterable, 0));assertEquals("b", Iterables.get(iterable, 1));assertEquals("c", Iterables.get(iterable, 2));try {Iterables.get(iterable, 3);fail("expected IndexOutOfBoundsException");} catch (IndexOutOfBoundsException e) {assertThat(e, hasToString("java.lang.IndexOutOfBoundsException: 3"));}}
}
- flatten用于将Iterable类型的Iterable进行flat操作;get方法则获取iterable中指定index的元素
小结
Iterables提供了concat、flatten、get三个静态方法,其中concat返回的是ConcatenatedIterable;flatten返回的是FlattenedIterables;get方法会先判断是否是List类型是的话直接通过position取值返回,不是则遍历iterable获取指定index的元素
doc
- Iterables
转载于:https://my.oschina.net/go4it/blog/3059398
聊聊Elasticsearch的Iterables相关推荐
- 聊聊Elasticsearch的ExponentiallyWeightedMovingAverage
序 本文主要研究一下Elasticsearch的ExponentiallyWeightedMovingAverage ExponentiallyWeightedMovingAverage elasti ...
- 聊聊elasticsearch的RoutingService
为什么80%的码农都做不了架构师?>>> 序 本文主要研究一下elasticsearch的RoutingService RoutingService elasticsearch ...
- 聊聊Elasticsearch的TimedRunnable
序 本文主要研究一下Elasticsearch的TimedRunnable TimedRunnable elasticsearch-7.0.1/server/src/main/java/org/ela ...
- 聊聊Elasticsearch RestClient的RequestLogger
序 本文主要研究一下Elasticsearch RestClient的RequestLogger RequestLogger elasticsearch-7.0.1/client/rest/src/m ...
- 聊聊Elasticsearch的CachedSupplier
序 本文主要研究一下Elasticsearch的CachedSupplier CachedSupplier elasticsearch-7.0.1/server/src/main/java/org/e ...
- 聊聊Elasticsearch的BootstrapCheck
序 本文主要研究一下Elasticsearch的BootstrapCheck BootstrapCheck elasticsearch-7.0.1/server/src/main/java/org/e ...
- 聊聊Elasticsearch的NodesSniffer
序 本文主要研究一下Elasticsearch的NodesSniffer NodesSniffer elasticsearch-7.0.1/client/sniffer/src/main/java/o ...
- 聊聊Elasticsearch的RunOnce
序 本文主要研究一下Elasticsearch的RunOnce RunOnce elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/c ...
- 【ElasticSearch从入门到放弃系列 零】ElasticSearch看这一篇就够了
大数据时代系统和业务每分每秒都产生成千上万的数据,其存储一定是不能通过关系型数据库了,当然因为数据的持久性也不能存储到内存型Nosql数据库Redis中,我们通常会将这些数据存储在能够不丢失数据的非关 ...
最新文章
- Udacity机器人软件工程师课程笔记(十六)-机械臂仿真控制实例(其一)-Gazebo、RViz和Moveit!
- python 30分钟_Python 30分钟入门指南
- 业务工作流平台设计(七)
- Harvard's CS50
- 3. Leetcode 16. 最接近的三数之和 (数组-双向双指针)
- poj 2528 Mayor's posters (线段树+离散化)
- es6中的块级作用域
- 洛谷 P3184 [USACO16DEC]Counting Haybales数草垛
- 容器编排技术 -- Kubernetes Pod 生命周期
- 华为轮值董事长郭平2020全联接大会主题演讲:永远面向阳光,阴影甩在身后
- java string.interned_Java中的字符串表示形式
- Anaconda 国内镜像配置
- “千脑智能理论”或颠覆AI,比尔·盖茨重磅推荐
- 计算机毕业设计ssm汽车租赁系统42876系统+程序+源码+lw+远程部署
- JavaScript 3D球形标签云代码
- Qt Cmake configuration has no path to a C++ compiler set, even though the toolkit has a v
- 如何将苹果手机中的M4A音乐转换为MP3格式
- Vue2源码解析 虚拟dom简介
- DOS计算机设置登录密码,电脑开机的时候有个DOS的密码怎么设置啊、
- python取整符号_python 取整
热门文章
- CDOJ 1805 矩阵 数学
- SpringMVC使用ModelAndView进行重定向
- Nginx启动报错误unlink() “nginx.pid” failed (2: No such file or directory)
- FCLK、HCLK、PCLK
- (转)教你记住ASP.NET WebForm页面的生命周期
- NSArray 所有基础点示例
- 解决IE6下CSS兼容性的两把神器
- Oracle中group by用法
- 基于VisualC++2010开发Windows7杀毒应用程序范例(2)---检测并遍历所有进程的线程信息...
- apipost使用mock随机获取多组数据中的一组数据进行测试