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基金会下 ...
随机推荐
- (转载)Total Commander 常用快捷键(并附快捷键大全)
(转载)http://blog.chinaunix.net/uid-532511-id-3051990.html Total Commander 常用快捷键 喜欢用Total Commander的人, ...
- selenium webdriver(6)---cookie相关操作
介绍selenium操作cookie之前,先简单介绍一下cookie的基础知识 cookie cookie一般用来识别用户身份和记录用户状态,存储在客户端电脑上.IE的cookie文件路径(win7) ...
- [置顶]VC2013的一个bug
[置顶]VC2013的一个bug 前段时间在尝试使用一个C++的GUI库nana.这个库最大的特点在于使用现代C++风格去编写GUI程序,而不需要使用大量的比较丑陋的代码(如MFC中的各种宏),或者其 ...
- 最佳化常用测试函数 Optimization Test functions
http://www.sfu.ca/~ssurjano/optimization.html The functions listed below are some of the common func ...
- HDOJ/HDU 1180 诡异的楼梯(经典BFS-详解)
Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯在 ...
- JavaScript---网络编程(5)-自定义对象Json、Dom模型概念讲解
这节博客主要讲解Dom模型概念~和JSON的简单介绍 首先,还是先上out.js的代码: function println(param){ document.write(param+"< ...
- Android 播放视频并获取指定时间的帧画面
最近做的项目要求既能播放视频(类似于视频播放器),又能每隔1s左右获取一帧视频画面,然后对图片进行处理,调查了一周,也被折磨了一周,总算找到了大致符合要求的方法.首先对调查过程中涉及到的方法进行简单介 ...
- HTC仅限拨打紧急电话
问题描述: 我手上有台 HTC One V 没碰没撞,突然打不出电话,信号上显示一个叉,屏幕上显示“仅限拨打紧急电话” 解决办法:经百度,原来很多HTC机子都有这种情况,幸好不是硬件坏了,只需按以下步 ...
- [Yii][RBAC]Yii中应用RBAC完全指南
开端筹办 Yii供给了强大的设备机制和很多现成的类库.在Yii中应用RBAC是很简单的,完全不须要再写RBAC代码.所以筹办工作就是,打开编辑器,跟我来. 设置参数.建树数据库 在设备数组中,增长以下 ...
- BOM、DOM学习笔记——JavaScript
1.BOM的概述 browser object modal :浏览器对象模型. 浏览器对象:window对象. Window 对象会在 <body> 或 <fram ...