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

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

Linux join命令的使用及说明

《Linuxjoin命令的使用及说明》`join`命令用于在Linux中按字段将两个文件进行连接,类似于SQL的JOIN,它需要两个文件按用于匹配的字段排序,并且第一个文件的换行符必须是LF,`jo... 目录一. 基本语法二. 数据准备三. 指定文件的连接key四.-a输出指定文件的所有行五.-o指定输出

Java中Redisson 的原理深度解析

《Java中Redisson的原理深度解析》Redisson是一个高性能的Redis客户端,它通过将Redis数据结构映射为Java对象和分布式对象,实现了在Java应用中方便地使用Redis,本文... 目录前言一、核心设计理念二、核心架构与通信层1. 基于 Netty 的异步非阻塞通信2. 编解码器三、

Linux jq命令的使用解读

《Linuxjq命令的使用解读》jq是一个强大的命令行工具,用于处理JSON数据,它可以用来查看、过滤、修改、格式化JSON数据,通过使用各种选项和过滤器,可以实现复杂的JSON处理任务... 目录一. 简介二. 选项2.1.2.2-c2.3-r2.4-R三. 字段提取3.1 普通字段3.2 数组字段四.

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

一篇文章彻底搞懂macOS如何决定java环境

《一篇文章彻底搞懂macOS如何决定java环境》MacOS作为一个功能强大的操作系统,为开发者提供了丰富的开发工具和框架,下面:本文主要介绍macOS如何决定java环境的相关资料,文中通过代码... 目录方法一:使用 which命令方法二:使用 Java_home工具(Apple 官方推荐)那问题来了,

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置