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

devtools-beans-streaming包是devtools系列的工具包之一,它是一個對對象列表(或稱為結(jié)果集)進(jìn)行查詢或聚合等操作的解決方案, 提供了類似于C#中的LINQ功能

安裝:


        <dependency>
            <groupId>com.github.paganini2008</groupId>
            <artifactId>devtools-beans-streaming</artifactId>
            <version>2.0.3</version>
        </dependency>

兼容性:


jdk1.8+

如何使用?


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
    @ToString
    public static class Salesman {

        private String name;
        private String password;

        public Salesman(String name, String password) {
            this.name = name;
            this.password = password;
        }

    }

}

然后定義一個List, 生成一批對象,隨機(jī)賦值
比如:

    // 定義一個List
    private 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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容