代码拉取完成,页面将自动刷新
中文 / English
Java 中使用类似 C# 的 Linq 能力
C# Linq
可以通过最少的代码对数据源进行关联、筛选、排序和分组等操作。这些操作可以在单个查询中组合起来,以获得更复杂的结果,无需for循环与if分支操作数据,内置查询引擎性能卓越
From
Select
Distinct
、Join
、Where
、Group By
、Order By
、Limit
、Offset
、...
⚠️ 注意:操作的对象字段必须存在 get 方法便于 lambda 查找,建议配合 Lombok 的 @Getter 注解快速创建字段的 get 访问
<!--包内零外部依赖,体积仅仅50kb-->
<dependency>
<groupId>xyz.erupt</groupId>
<artifactId>linq.j</artifactId>
<version>0.0.4</version>
</dependency>
var strings = Linq.from("C", "A", "B", "B").gt(Th::is, "A").orderByDesc(Th::is).write(String.class);
// [C, B, B]
var integers = Linq.from(1, 2, 3, 7, 6, 5).orderBy(Th::is).write(Integer.class);
// [1, 2, 3, 5, 6, 7]
var name = Linq.from(data)
// left join
.innerJoin(target, Target::getId, Data::getId)
// where like
.like(Data::getName, "a")
// select name
.select(Data::getName)
// distinct
.distinct()
// order by
.orderBy(Data::getName)
.write(String.class);
public class ObjectQuery{
private final List<TestSource> source = http.get("https://gw.alipayobjects.com/os/antfincdn/v6MvZBUBsQ/column-data.json");
private final List<TestSourceExt> target = mongodb.query("db.target.find()");
/**
* select demo
*/
public void select(){
// select *
Linq.from(source).select(Columns.all(TestSource.class));
// select a, b, c
Linq.from(source)
.select(TestSource::getName, TestSource::getDate, TestSource::getTags)
.select(Columns.of(TestSource::getTags, "tag2")) // alias
.select(Columns.ofx(TestSource::getId, id -> id + "xxx")); // value convert
// select count(*), sum(id), max(id)
Linq.from(source)
.select(Columns.count("count"))
.select(Columns.sum(TestSource::getId, "sum"))
.select(Columns.max(TestSource::getId, "max"));
}
/**
* join demo
*/
public void join(){
// left join
Linq.from(source).leftJoin(target, TestSourceExt::getId, TestSource::getId).select(
Columns.all(TestSource.class),
Columns.of(TestSourceExt::getName),
Columns.of(TestSourceExt2::getValue)
);
// right join
Linq.from(source).rightJoin(target, TestSourceExt::getId, TestSource::getId);
// inner join
Linq.from(source).innerJoin(target, TestSourceExt::getId, TestSource::getId);
// full join
Linq.from(source).fullJoin(target, TestSourceExt::getId, TestSource::getId);
}
/**
* where demo
*/
public void where() {
// =
Linq.from(source).eq(TestSource::getName, "Thanos").select(Columns.count(countAlias)).writeOne(Integer.class);
// >=:lval and <=:rval
Linq.from(source).between(TestSource::getId, 1, 3);
// in (x,x,x)
Linq.from(source).in(TestSource::getId, 1, 2, 3);
// like '%x%'
Linq.from(source).like(TestSource::getName, "a");
// is null
Linq.from(source).isNull(TestSource::getId);
// customer single field where
Linq.from(source).where(TestSource::getId, id -> id >= 5);
// customer condition or multi field
Linq.from(source).condition(data -> {
String name = data.get(TestSource::getName);
Integer age = (Integer)data.get(TestSource::getAge);
// name = 'xxx' or age > 10
return "xxx".equals(name) || age > 10;
});
}
/**
* group by demo
*/
public void groupBy(){
Linq.from(source)
.groupBy(TestSource::getName)
.select(
Columns.of(TestSource::getName, "name"),
Columns.min(TestSource::getDate, "min"),
Columns.avg(TestSource::getId, "avg"),
Columns.count("count"),
Columns.count(TestSource::getName, "countName"),
Columns.countDistinct(TestSource::getName, "countDistinct")
)
.having(row -> Integer.parseInt(row.get("avg").toString()) > 2)
.orderBy(TestSource::getAge);
}
/**
* result write demo
*/
public void write(){
// write List<Object>
List<TestSource> list = Linq.from(source).orderByAsc(TestSource::getDate).write(TestSource.class);
// write Object
TestSource obj = Linq.from(source).limit(3).writeOne(TestSource.class);
// write List<Map>
List<Map<String, Object>> map = Linq.from(source).writeMap();
// write Map
Map<String, Object> mapOne = Linq.from(source).writeMapOne();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型