Java的jLinqer包介绍
1.介绍
2.Linqer简介
<dependency>
<groupId>com.github.jlinqer</groupId>
<artifactId>jlinqer</artifactId>
<version>1.0.0</version>
</dependency>
如果没有使用Maven 时,直接使用 jLinqer jar,下载地址:
https://oss.sonatype.org/content/groups/public/com/github/jlinqer/jlinqer/1.0.0/jlinqer-1.0.0.jar
4.LINQ jLinqer 对比
| 功能 | LINQ(C#) | jLinqer(Java) | Stream(Java) |
|---|---|---|---|
| 【基本】 | |||
| 筛选 | Where | where | filter |
| 投影 | Select | select | map |
| 排序(升序) | OrderBy | orderBy | sorted |
| 排序(降序) | OrderByDescending | orderByDescending | n/a |
| 按升序对序列中的元素执行后续排序 | ThenBy | thenBy | n/a |
| 按降序对序列中的元素执行后续排序。 | ThenByDescending | thenByDescending | n/a |
| 将序列的每个元素投影到 IEnumerable<T> 并将结果序列合并为一个序列。 | SelectMany | selectMany | flatMap |
| 【提取】 | |||
| 跳过序列中指定数量的元素,然后返回剩余的元素。 | Skip | skip | skip |
| 只要满足指定的条件,就跳过序列中的元素,然后返回剩余元素。 | SkipWhile | skipWhile | n/a |
| 从序列的开头返回指定数量的连续元素。 | Take | take | limit |
| 只要满足指定的条件,就会返回序列的元素,然后跳过剩余的元素。 | TakeWhile | takeWhile | n/a |
| 【合成】 | |||
| 连接两个序列 | Concat | concat | concat |
| 生成两个序列的交集 | Intersect | intersect | n/a |
| 生成两个序列的并集 | Union | union | n/a |
| 生成两个序列的差集 | Except | except | n/a |
| 基于匹配键对两个序列的元素进行关联 | Join | join | n/a |
| 基于键相等对两个序列的元素进行关联并对结果进行分组 | GroupJoin | groupJoin | n/a |
| 反转序列中元素的顺序 | Reverse | reverse | n/a |
| 将指定函数应用于两个序列的对应元素,以生成结果序列 | Zip | zip | n/a |
| 【分组和总计】 | |||
| 返回序列中的非重复元素 | Distinct | distinct | distinct |
| 累加器函数 | Aggregate | aggregate | reduce |
| 对序列中的元素进行分组 | GroupBy | groupBy | Collectors.groupingBy |
| 计算数值序列的平均值 | Average | averageXXX | Collectors.summarizingXXX |
| 返回序列中的元素数量 | Count | count | n/a |
| 返回一个 Int64,表示序列中的元素的数量 | LongCount | longCount | count |
| 返回值序列中的最大值 | Max | max | max |
| 返回值序列中的最小值 | Min | min | min |
| 计算数值序列之和 | Sum | sumXXX | Collectors.summarizingXXX |
| 返回序列中的第一个元素 | First | first | findFirst |
| 返回序列中的第一个元素;如果未找到元素,则返回默认值 | FirstOrDefault | firstOrDefault | n/a |
| 返回序列的最后一个元素 | Last | last | n/a |
| 返回序列中的最后一个元素;如果未找到元素,则返回默认值 | LastOrDefault | lastOrDefault | n/a |
| 返回值序列的单个特定元素 | Single | single | n/a |
| 返回序列中单个特定元素,如果未找到这样的元素,则返回默认值 | SingleOrDefault | singleOrDefault | n/a |
| 返回指定序列的元素;如果序列为空,则返回集合中的类型参数的默认值。 | DefaultIfEmpty | defaultIfEmpty | n/a |
| 返回序列中指定索引处的元素 | ElementAt | elementAt | n/a |
| 返回序列中指定索引处的元素;如果索引超出范围,则返回默认值 | ElementAtOrDefault | elementAtOrDefault | n/a |
| 确定序列中的所有元素是否满足条件 | All | all | allMatch |
| 确定序列中的任何元素是否存在或满足条件 | Any | any | anyMatch |
| 【区间】 | |||
| 返回一个具有指定的类型参数的空 | Empty | empty | n/a |
| 生成指定范围内的整数的序列 | Range | range | n/a |
| 生成包含一个重复值的序列 | Repeat | repeat | n/a |
| 【其他】 | |||
| 根据相等比较器确定两个序列是否相等 | SequenceEqual | sequenceEqual | n/a |
| 强制转换为指定的类型 | Cast | cast | n/a |
| 根据指定类型筛选 IEnumerable 的元素 | OfType | ofType | n/a |
5.使用方法
Where
List<Integer> list = new List<>(1, 2, 3); List<Integer> actual = list.where(x -> x == 1 || x == 3).toList(); assertEquals(true , actual.contains(1));
assertEquals(false, actual.contains(2));
assertEquals(true , actual.contains(3));
Select
List<Person> list = new List<>(
new Person("React" , 1),
new Person("Angular" , 3),
new Person("Backbone", 5)
); List<String> actual = list.select(x -> x.name).toList(); assertEquals("React" , actual.get(0));
assertEquals("Angular" , actual.get(1));
assertEquals("Backbone", actual.get(2));
OrderBy
List<String> list = new List<>("Backbone", "Angular", "React");
List<String> actual = list.orderBy(x -> x).toList();
assertEquals("Angular" , actual.get(0));
assertEquals("Backbone", actual.get(1));
assertEquals("React" , actual.get(2));
OrderByDescending
List<String> list = new List<>("Backbone", "Angular", "React");
List<String> actual = list.orderByDescending(x -> x).toList();
assertEquals("React" , actual.get(0));
assertEquals("Backbone", actual.get(1));
assertEquals("Angular" , actual.get(2));
ThenBy
List<Person> list = new List<>(
new Person("Angular2", 2),
new Person("Angular1", 2),
new Person("React" , 1)
); List<String> actual = list.orderBy(x -> x.age).thenBy(x -> x.name).toList(); assertEquals("React" , actual.get(0).name);
assertEquals("Angular1", actual.get(1).name);
assertEquals("Angular2" , actual.get(2).name);
ThenByDescending
List<Person> list = new List<>(
new Person("Angular2", 2),
new Person("Angular1", 2),
new Person("React" , 1)
); List<String> actual = list.orderBy(x -> x.age).thenByDescending(x -> x.name).toList(); assertEquals("React" , actual.get(0).name);
assertEquals("Angular2", actual.get(1).name);
assertEquals("Angular1" , actual.get(2).name);
SelectMany
List<Person> list = new List<>(
new Person("Angular", 3, new List("1.0.1", "1.0.2")),
new Person("React" , 1, new List("2.0.1", "2.0.2"))
); List<String> actual = list.selectMany(x -> x.versionHistory).toList(); assertEquals("1.0.1", actual.get(0));
assertEquals("1.0.2", actual.get(1));
assertEquals("2.0.1", actual.get(2));
assertEquals("2.0.2", actual.get(3));
Skip
List<Integer> list = new List<>(1, 2, 3); List<Integer> actual = list.skip(2).toList(); assertEquals(3, actual.get(0).intValue());
SkipWhile
List<Integer> list = new List<>(1, 2, 3, 4, 5); List<Integer> actual = list.skipWhile(x -> x <= 3).toList(); assertEquals(4, actual.get(0).intValue());
assertEquals(5, actual.get(1).intValue());
Take
List<String> list = new List<>("Backbone", "Angular", "React");
List<String> actual = list.take(2).toList();
assertEquals(2, actual.size());
assertEquals("Backbone", actual.get(0));
assertEquals("Angular" , actual.get(1));
TakeWhile
List<String> list = new List<>("Backbone", "Angular", "React");
List<String> actual = list.takeWhile(x -> x.equals("Backbone") || x.equals("Angular")).toList();
assertEquals(2, actual.size());
assertEquals("Backbone", actual.get(0));
assertEquals("Angular" , actual.get(1));
Concat
List<Integer> first = new List<>(1, 2);
List<Integer> second = new List<>(2, 3); List<Integer> actual = first.concat(second).toList(); assertEquals(1, actual.get(0).intValue());
assertEquals(2, actual.get(1).intValue());
assertEquals(2, actual.get(2).intValue());
assertEquals(3, actual.get(3).intValue());
Intersect
List<Integer> first = new List<>(1, 2, 3);
List<Integer> second = new List<>(1, 3); List<Integer> actual = first.intersect(second).toList(); assertEquals(1, actual.get(0).intValue());
assertEquals(3, actual.get(1).intValue());
Union
List<Integer> first = new List<>(1, 2, 3);
List<Integer> second = new List<>(0, 1, 3, 4); List<Integer> actual = first.union(second).toList(); assertEquals(5, actual.size());
assertEquals(1, actual.get(0).intValue());
assertEquals(2, actual.get(1).intValue());
assertEquals(3, actual.get(2).intValue());
assertEquals(0, actual.get(3).intValue());
assertEquals(4, actual.get(4).intValue());
Except
List<Integer> first = new List<>(1, 2, 3);
List<Integer> second = new List<>(1, 3); List<Integer> actual = first.except(second).toList(); assertEquals(2, actual.get(0).intValue());
Join
List<Javascript> outer = new List<>(
new Javascript("Angular", 1),
new Javascript("React" , 4),
new Javascript("ES2016" , 5)
);
List<Javascript> inner = new List<>(
new Javascript("Angular", 2),
new Javascript("Angular", 3),
new Javascript("ES2016" , 6),
new Javascript("ES7" , 7)
); Function<Javascript, String> outerKey = (x) -> x.name;
Function<Javascript, String> innerKey = (y) -> y.name;
BiFunction<Javascript, Javascript, Javascript> selector = (x, y) -> new Javascript(x.name, y.age);
List<Javascript> actual = outer.join(inner, outerKey, innerKey, selector).toList(); assertEquals(3, actual.size());
assertEquals("Angular", actual.get(0).name);
assertEquals("Angular", actual.get(1).name);
assertEquals("ES2016" , actual.get(2).name);
assertEquals(2, actual.get(0).age);
assertEquals(3, actual.get(1).age);
assertEquals(6, actual.get(2).age);
GroupJoin
List<Javascript> outer = new List<>(
new Javascript("Angular", 1),
new Javascript("React" , 4),
new Javascript("ES2016" , 5)
);
List<Javascript> inner = new List<>(
new Javascript("Angular", 2),
new Javascript("Angular", 3),
new Javascript("ES2016" , 6),
new Javascript("ES7" , 7)
); Function<Javascript, String> outerKey = (x) -> x.name;
Function<Javascript, String> innerKey = (y) -> y.name;
BiFunction<Javascript, IEnumerable<Javascript>, Javascript> selector = (x, y) -> new Javascript(x.name, y.select(z -> z.age));
List<Javascript> actual = outer.groupJoin(inner, outerKey, innerKey, selector).toList(); assertEquals(3, actual.size());
assertEquals("Angular", actual.get(0).name);
assertEquals("React" , actual.get(1).name);
assertEquals("ES2016" , actual.get(2).name);
assertEquals(2, actual.get(0).ages.elementAt(0));
assertEquals(3, actual.get(0).ages.elementAt(1));
assertEquals(0, actual.get(1).ages.count());
assertEquals(6, actual.get(2).ages.elementAt(0));
Reverse
List<Integer> list = new List<>(1, 2, 3); List<Integer> actual = list.reverse().toList(); assertEquals(3, actual.get(0).intValue());
assertEquals(2, actual.get(1).intValue());
assertEquals(1, actual.get(2).intValue());
Zip
List<Integer> first = new List<>(1, 2, 3);
List<String> second = new List<>("Angular", "React", "Backbone"); List<Integer> actual = first.zip(second, (x, y) -> String.format("%s %d", x, y)).toList(); assertEquals("1 Angular" , actual.get(0));
assertEquals("2 React" , actual.get(1));
assertEquals("3 Backbone", actual.get(2));
Distinct
List<Integer> list =new List<>(1, 2, 3,1, 2, 3, 4); List<Integer> actual = list.distinct().toList(); assertEquals(1, actual.get(0).intValue());
assertEquals(2, actual.get(1).intValue());
assertEquals(3, actual.get(2).intValue());
assertEquals(4, actual.get(3).intValue());
Aggregate
List<Integer> list = new List<>(1, 2, 3); int actual = list.aggregate((sum, elem) -> sum + elem); assertEquals(6, actual);
GroupBy
List<Person> list = new List<>(
new Person("React" , 1),
new Person("Angular" , 1),
new Person("Backbone", 5)
); Map<Integer, IEnumerable<Person>> actual = list.groupBy(x -> x.age); assertEquals(true, actual.get(1).any(x -> x.name.equals("React")));
assertEquals(true, actual.get(1).any(x -> x.name.equals("Angular")));
assertEquals(true, actual.get(5).any(x -> x.name.equals("Backbone")));
Average
List<Long> listLong = new List<>(1l, 2l, 3l, 4l); double actualLong = listLong.averageLong(x -> x); assertEquals(2.5d, actualLong, 0);
Count
List<String> list = new List<>("Backbone", "Angular", "React");
long actual = list.longCount();
int actualNone = list.count(x -> x.equals("jquery"));
assertEquals(3, actual);
assertEquals(0, actualNone);
Max
List<Double> listDouble = new List<>(1d, 2d, 3d); double actualDouble = listDouble.max(x -> x); assertEquals(3d, actualDouble, 0);
Min
List<BigDecimal> listBigDecimal = new List<>(
new BigDecimal(1d),
new BigDecimal(2d),
new BigDecimal(3d)
); BigDecimal actualBigDecimal = listBigDecimal.min(x -> x); assertEquals(1d, actualBigDecimal.doubleValue(), 0);
Sum
List<Integer> listInt = new List<>(1, 2, 3); int actualInt = listInt.sumInt(x -> x); assertEquals(6, actualInt);
FirstOrDefault
List<String> list = new List<>("Backbone", "Angular", "React");
String actualFirst = list.firstOrDefault();
String actualMatch = list.firstOrDefault(x -> x.equals("Angular"));
String actualUnMatch = list.firstOrDefault(x -> x.equals("jquery"));
assertEquals("Backbone", actualFirst);
assertEquals("Angular" , actualMatch);
assertEquals(null , actualUnMatch);
LastOrDefault
List<Integer> list = new List<>(1, 2, 3);
List<Integer> listEmpty = new List<>(); int actual = list.lastOrDefault();
Integer actualDefaultNone = listEmpty.lastOrDefault(x -> x == 0); assertEquals(3, actual);
assertEquals(null, actualDefaultNone);
SingleOrDefault
List<Integer> listMany = new List<>(1, 2, 3);
List<Integer> listEmpty = new List<>(); int actualFilter = listMany.singleOrDefault(x -> x == 3);
Integer actualUnMatch = listEmpty.singleOrDefault(x -> x == 0); assertEquals(3, actualFilter);
assertEquals(null, actualUnMatch);
DefaultIfEmpty
List<String> listEmpty = new List<>();
List<String> actualDefault = listEmpty.defaultIfEmpty("ES7").toList();
assertEquals("ES7", actualDefault.get(0));
ElementAtOrDefault
List<Integer> list = new List<>(1, 2, 3);
int actual = list.elementAtOrDefault(2);
Integer actualDefault = list.elementAtOrDefault(3);
assertEquals(3, actual);
assertEquals(null, actualDefault);
All
List<String> list = new List<>("Backbone", "Angular", "React");
boolean actual = list.all(x -> x.equals("Angular") || x.equals("Backbone") || x.equals("React"));
boolean actualNotFound = list.all(x -> x.equals("Angular") || x.equals("React"));
assertEquals(true, actual);
assertEquals(false, actualNotFound);
Any
List<String> list = new List<>("Backbone", "Angular", "React");
boolean actual = list.any(x -> x.equals("Angular"));
boolean actualNotFound = list.any(x -> x.equals("jquery"));
assertEquals(true, actual);
assertEquals(false, actualNotFound);
Empty
List<Double> actual = IEnumerable.empty(Double.class); assertEquals(0, actual.count());
Range
List<Integer> actual = IEnumerable.range(-2, 3); assertEquals(-2, actual.get(0).intValue());
assertEquals(-1, actual.get(1).intValue());
assertEquals(0 , actual.get(2).intValue());
Repeat
List<String> actual = IEnumerable.repeat(String.class, "Law of Cycles", 10); assertEquals(10, actual.count());
assertEquals("Law of Cycles", actual.get(9));
SequenceEqual List<Integer> first = new List<>(1, 2, 3);
List<Integer> secondMatch = new List<>(1, 2, 3);
List<Integer> secondUnMatchElem = new List<>(1, 2, 4); boolean actualMatch = first.sequenceEqual(secondMatch);
boolean actualUnMatchElm = first.sequenceEqual(secondUnMatchElem); assertEquals(true, actualMatch);
assertEquals(false, actualUnMatchElm);
Cast
List<Object> list = new List<>(1, 2, 3); List<Integer> actual = list.cast(Integer.class).toList(); assertEquals(1, actual.get(0).intValue());
assertEquals(2, actual.get(1).intValue());
assertEquals(3, actual.get(2).intValue());
OfType
List<Object> list = new List<>(1, "2", 3, "4"); List<String> actualStr = list.ofType(String.class).toList();
List<Integer> actualInt = list.ofType(Integer.class).toList(); assertEquals("2", actualStr.get(0));
assertEquals("4", actualStr.get(1));
assertEquals(1 , actualInt.get(0).intValue());
assertEquals(3 , actualInt.get(1).intValue());
6.源代码
https://github.com/k--kato/jLinqer
Java的jLinqer包介绍的更多相关文章
- 杂项-Java:jar 包与 war 包介绍与区别
ylbtech-杂项-Java:jar 包与 war 包介绍与区别 1.返回顶部 1. 做Java开发,jar包和war包接触的挺多的,有必要对它们做一个深入的了解,特总结整理如下: 1.jar包的介 ...
- Java语言Lang包下常用的工具类介绍_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 无论你在开发哪中 Java 应用程序,都免不了要写很多工具类/工具函数.你可知道,有很多现成的工具类可用,并且代码质量都 ...
- java中的包以及内部类的介绍
1:形式参数和返回值的问题(理解) (1)形式参数: 类名:需要该类的对象 抽象类名:需要该类的子类对象 接口名:需要该接口的实现类对象 (2)返 ...
- java基础 lang包 详细介绍
Java.javax和org.其中以java开头的包名是JDK的基础语言包,以javax开头的属 (org是organization的简写).而在JDK API中还包含了一些以com.sun开头的包名 ...
- 线程并发线程安全介绍及java.util.concurrent包下类介绍
线程Thread,在Java开发中多线程是必不可少的,但是真正能用好的并不多! 首先开启一个线程三种方式 ①new Thread(Runnable).start() ②thread.start(); ...
- 深入探讨 java.lang.ref 包
深入探讨 java.lang.ref 包 本文主要探讨了 java.lang.ref 包的使用方法,以及源码解读.并就该包在不同 JVM 上的表现进行了比较与分析.通过阅读本文,读者可以加深对 jav ...
- 深入探讨 java.lang.ref 包--转
概述 Java.lang.ref 是 Java 类库中比较特殊的一个包,它提供了与 Java 垃圾回收器密切相关的引用类.这些引用类对象可以指向其它对象,但它们不同于一般的引用,因为它们的存在并不防碍 ...
- 家庭洗车APP --- Androidclient开展 之 网络框架包介绍(一)
家庭洗车APP --- Android客户端开发 之 网络框架包介绍(一) 上篇文章中给大家简单介绍了一些业务.上门洗车APP --- Android客户端开发 前言及业务简单介绍,本篇文章给大家介绍 ...
- java web服务器tomcat介绍【转载】
机器矩阵2016-08-10 22:14 java程序员亲切地称他为tom猫,看到这只猫可以说明1 服务器部署成功了 ,2 网络是联通的. 到底这只猫是什么来头呢? tomcat是Apache基金会下 ...
随机推荐
- (转载)PHP获取客户端、PHP获取服务器相关信息
(转载)http://www.php100.com/html/webkaifa/PHP/PHP/2009/1027/3446.html 服务器变量 $_SERVER 详解: 1.$_SESSION[' ...
- Colour your Log4Net events in your RichTextBox zz
You’re most probably here because you have already read my article How to watch your log through you ...
- 检测Office是否安装以及获取安装 路径 及安装版本 QQ,迅雷,旺旺 C#代码
#region 检测Office是否安装 ///<summary> /// 检测是否安装office ///</summary> ///<param name=" ...
- HDU 1280 前m大的数
http://acm.hdu.edu.cn/showproblem.php?pid=1280 前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- 作品第一课----循环改变DIV颜色
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- UVA 10652 Board Wrapping(凸包)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32286 [思路] 凸包 根据角度与中心点求出长方形所有点来,然后就 ...
- 研磨设计模式解析及python代码实现——(三)适配器模式(Adapter)
一.适配器模式定义 将一个类的接口转换成另外一个接口,适配器模式使得原本由于接口不兼容,而不能在一起工作的哪些类能够在一起工作. 二.python 实现 import string import cP ...
- hdu1698(线段树的区间替换)
HDU1698 #include <bits/stdc++.h> using namespace std; #define Maxn 1001000*4 struct Node{ int ...
- PG数据库之间的导入导出
本文将介绍如何对PG数据库进行导入.导出,主要利用的是PG自带的pg_dump.pg_dumpall.pg_restore.psql等命令,版本是9.4(不同版本的pg_dump \ pg_resto ...
- web 网站安全证书已过期或不可信 是否继续浏览
发生环境:魅族MX4 uc浏览器 IIS部署SSL证书后提示不可信的解决方案 第一步:打开mmc——点击文件——添加删除管理单元——证书——计算机帐户 第二步:在计算机帐户的个人证书里面导入pfx格 ...