QLExpress相关文档可以参考:文档连接
由于网上资料很少,都是demo相互照搬。没有我想要的需求demo,所以看文档写了QLExpress对集合进行并集,交集等操作的demo,抛砖引玉,仅供参考。
import cn.hutool.json.JSONUtil;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.Operator;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
/**
*
* @date : 2022/12/12 16:39
*/
public class SubOperator {
/**
* 两个集合 交集
* 判空操作忽略,请自行补充
*/
static class ListSubOperator extends Operator {
public Object executeInner(Object[] list) {
Set<Long> set1 = (Set<Long>) list[0];
Set<Long> set2 = (Set<Long>) list[1];
return set1.stream().filter(set2::contains).collect(Collectors.toSet());
}
}
/**
* 两个集合并集
* 判空操作忽略,请自行补充
*/
static class ListAddOperator extends Operator {
public Object executeInner(Object[] list) {
Set<Long> opdata1 = (Set<Long>) list[0];
Set<Long> opdata2 = (Set<Long>) list[1];
opdata1.addAll(opdata2);
return opdata1;
}
}
public static void main(String[] args) throws Exception {
ExpressRunner runner = new ExpressRunner();
DefaultContext<String, Object> context = new DefaultContext<String, Object>();
runner.addOperator("addAll", new ListAddOperator());
runner.addOperator("sub", new ListSubOperator());
Set<Long> set1 = new HashSet<>();
Set<Long> set2 = new HashSet<>();
Set<Long> set3 = new HashSet<>();
set1.add(1L);
set1.add(2L);
set1.add(3L);
set2.add(2L);
set2.add(4L);
set2.add(6L);
set3.add(1L);
set3.add(6L);
set3.add(8L);
context.put("a", set1);
context.put("b", set2);
context.put("c", set3);
//
Object r = runner.execute("(a addAll b) sub c", context, null, false, false);
System.out.println(JSONUtil.toJsonStr(r));
}
}