Java版LINQ工具包devtools-beans-streaming使用介绍

2024-04-15 18:18

本文主要是介绍Java版LINQ工具包devtools-beans-streaming使用介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

devtools-beans-streaming包是devtools系列的工具包之一,它是一个对对象列表(或称为结果集)进行查询或聚合等操作的解决方案, 提供了类似于C#中的LINQ功能

Maven:
		<dependency><groupId>com.github.paganini2008</groupId><artifactId>devtools-beans-streaming</artifactId><version>2.0.2</version></dependency>
Jdk版本:

jdk 1.8+

API使用说明:

devtools-beans-streaming操作的对象通常是一个List

举例说明:

比如有这样一个对象:

@Getter
@Setter
@ToString
public class Product {private int id;private String name;private String location;private Date created;private Date expired;private Float price;private BigInteger sales;private boolean export;private Long number;private BigDecimal freight;private Style style;private Salesman salesman;public static enum Style {HARD, SOFT;}@Getter@Setter@ToStringpublic static class Salesman {private String name;private String password;public Salesman(String name, String password) {this.name = name;this.password = password;}}}

然后定义一个List, 生成一批对象,随机赋值
比如:

    // 定义一个Listprivate static final List<Product> products = new ArrayList<Product>(); static {String[] users = new String[] { "Petter", "Jack", "Tony" };String[] locations = new String[] { "London", "NewYork", "Tokyo", "HongKong", "Paris" };for (int i = 0; i < 10000; i++) {Product product = new Product();product.setCreated(DateUtils.valueOf(2020, RandomUtils.randomInt(1, 12), RandomUtils.randomInt(1, 28)));product.setExpired(DateUtils.addDays(product.getCreated(), 90));product.setExport(RandomUtils.randomBoolean());product.setFreight(BigDecimal.valueOf(RandomUtils.randomFloat(10, 100)).setScale(2, RoundingMode.HALF_UP));product.setId(i + 1);product.setLocation(RandomUtils.randomChoice(locations));product.setName("Product-" + product.getId());product.setNumber(RandomUtils.randomLong(1, 1000));product.setPrice(RandomUtils.randomFloat(10, 1000, 2));product.setSales(BigInteger.valueOf(RandomUtils.randomLong(10000, 100000)));product.setStyle(Style.values()[RandomUtils.randomInt(0, 2)]);product.setSalesman(new Product.Salesman(RandomUtils.randomChoice(users), "123456"));products.add(product);}}
示例1
		Predicate<Product> predicate = Restrictions.eq("location", "London");Selector.from(products).filter(predicate).list().forEach(product -> {System.out.println(product);});
// Equivalent to: select * from Product where location='London'
示例2
		Predicate<Product> predicate = Restrictions.lte("created", new Date());predicate = predicate.and(Restrictions.eq("salesman.name", "Petter"));Selector.from(products).filter(predicate).list().forEach(product -> {System.out.println(product);});
// select * from Product where created<= now() and salesman.name='Petter'
示例3
        Selector.from(products).groupBy("location", String.class).setTransformer(new View<Product>() {protected void setAttributes(Tuple tuple, Group<Product> group) {tuple.set("maxPrice", group.max("price", Float.class));tuple.set("minPrice", group.min("price", Float.class));tuple.set("avgFreight", group.avg("freight"));tuple.set("sumSales", group.sum("sales"));}}).list().forEach(tuple -> {System.out.println(tuple);});
// Equivalent to: select location,max(price) as maxPrice, min(price) as minPrice,avg(freight) as avgFreight,sum(sales) as sumSales from Product group by location
示例4
		Selector.from(products).groupBy("location", String.class).groupBy("style", Product.Style.class).having(group -> {return group.avg("freight").compareTo(BigDecimal.valueOf(55)) > 0;}).setTransformer(new View<Product>() {protected void setAttributes(Tuple tuple, Group<Product> group) {tuple.set("maxPrice", group.max("price", Float.class));tuple.set("minPrice", group.min("price", Float.class));tuple.set("avgFreight", group.avg("freight"));tuple.set("sumSales", group.sum("sales"));}}).list().forEach(tuple -> {System.out.println(tuple);});
// Equivalent to: select location,style,max(price) as maxPrice, min(price) as minPrice,avg(freight) as avgFreight,sum(sales) as sumSales from Product group by location,style having avg(freight) > 55
示例5:
		Sorter<Product> sorter = Orders.descending("price", BigDecimal.class);Selector.from(products).orderBy(sorter).list(100).forEach(product -> {System.out.println("Name: " + product.getName() + ", Price: " + product.getPrice() + ", Freight: " + product.getFreight());});
// Equivalent to: select name,price from Product order by price desc limit 100

源码地址:https://github.com/paganini2008/devtools.git

这篇关于Java版LINQ工具包devtools-beans-streaming使用介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/906560

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关