<article class=“article fmt article-content”><h2>拓展浏览</h2><blockquote><p>linq</p><p>querydsl</p></blockquote><h2>LINQ</h2><p>术语“LINQ to Objects”指间接将 LINQ 查问与任何 <code>IEnumerable<T></code> 汇合一起应用。 </p><p>能够应用 LINQ 来查问任何可枚举的汇合,例如 Primitive Array、Object Array、 List、 Collection 或 Iterable 等等。 </p><p>该汇合能够是用户定义的汇合,也能够是由 Java 开发包 API 返回的汇合。</p><p>从根本上说,“LINQ to Objects”示意一种新的解决汇合的办法。 采纳 LINQ 办法,只需编写形容要检索的内容的申明性代码。</p><h3>个性</h3><ul><li>实现了 LINQ to Objects 的所有 API。</li><li>反对更多 API 和元组。</li><li>反对 IEnumerable 和 Stream 相互转换。</li><li>反对 Android。</li></ul><h2>入门例子</h2><h3>Maven</h3><pre><code class=“xml”><dependency> <groupId>com.bestvike</groupId> <artifactId>linq</artifactId> <version>6.0.0</version></dependency></code></pre><h3>用法</h3><p>如果应用 java 8 或 java 9,倡议用 lombok.var 或 lombok.val 代替简单的返回类型。 </p><p>如果应用 java 10 或更高版本,倡议应用 var 代替简单的返回类型。</p><p>拼接不为空的字符串。</p><pre><code class=“java”>String result = Linq.of("!@#$%^", “C”, “AAA”, “”, “Calling Twice”, “SoS”, Empty) .where(x -> x != null && x.length() > 0) .aggregate((x, y) -> x + “, " + y);System.out.println(result);—-!@#$%^, C, AAA, Calling Twice, SoS</code></pre><p>判断所有的负数是否全副为偶数。</p><pre><code class=“java”>boolean result = Linq.of(9999, 0, 888, -1, 66, -777, 1, 2, -12345) .where(x -> x > 0) .all(x -> x % 2 == 0);System.out.println(result);—-false</code></pre><p>判断所有的负数是否存在任一偶数。</p><pre><code class=“java”>boolean result = Linq.of(9999, 0, 888, -1, 66, -777, 1, 2, -12345) .where(x -> x > 0) .any(x -> x % 2 == 0);System.out.println(result);—-true</code></pre><p>在开端追加一个数字并在头部插入两个数字。</p><pre><code class=“java”>String result = Linq.range(3, 2).append(5).prepend(2).prepend(1).format();System.out.println(result);—-[1, 2, 3, 4, 5]</code></pre><p>计算整数序列的平均值。</p><pre><code class=“java”>double result = Linq.of(5, -10, 15, 40, 28).averageInt();System.out.println(result);—-15.6</code></pre><p>连贯两个整数序列。</p><pre><code class=“java”>String result = Linq.of(1, 2).concat(Linq.of(3, 4)).format();System.out.println(result);—-[1, 2, 3, 4]</code></pre><h2>参考资料</h2><p>https://github.com/timandy/linq</p><blockquote>本文由博客一文多发平台 OpenWrite 公布!</blockquote></article>