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

相关文章

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

SpringBoot应用中出现的Full GC问题的场景与解决

《SpringBoot应用中出现的FullGC问题的场景与解决》这篇文章主要为大家详细介绍了SpringBoot应用中出现的FullGC问题的场景与解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录Full GC的原理与触发条件原理触发条件对Spring Boot应用的影响示例代码优化建议结论F

springboot项目中常用的工具类和api详解

《springboot项目中常用的工具类和api详解》在SpringBoot项目中,开发者通常会依赖一些工具类和API来简化开发、提高效率,以下是一些常用的工具类及其典型应用场景,涵盖Spring原生... 目录1. Spring Framework 自带工具类(1) StringUtils(2) Coll

Python虚拟环境终极(含PyCharm的使用教程)

《Python虚拟环境终极(含PyCharm的使用教程)》:本文主要介绍Python虚拟环境终极(含PyCharm的使用教程),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录一、为什么需要虚拟环境?二、虚拟环境创建方式对比三、命令行创建虚拟环境(venv)3.1 基础命令3

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi

关于pandas的read_csv方法使用解读

《关于pandas的read_csv方法使用解读》:本文主要介绍关于pandas的read_csv方法使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录pandas的read_csv方法解读read_csv中的参数基本参数通用解析参数空值处理相关参数时间处理相关

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

SpringBoot条件注解核心作用与使用场景详解

《SpringBoot条件注解核心作用与使用场景详解》SpringBoot的条件注解为开发者提供了强大的动态配置能力,理解其原理和适用场景是构建灵活、可扩展应用的关键,本文将系统梳理所有常用的条件注... 目录引言一、条件注解的核心机制二、SpringBoot内置条件注解详解1、@ConditionalOn