This is a sneak peek of an exciting new data management technology. linq4j (short for “Language-Integrated Query for Java”) is inspired by Microsoft’s LINQ technology, previously only available on the .NET platform, and adapted for Java. (It also builds upon ideas I had in my earlier Saffron project.)

I launched the linq4j project less than a week ago, but already you can do select, filter, join and groupBy operations on in-memory and SQL data.

In this demo, I write and execute sample code against the working system, and explain the differences between the key interfaces Iterable, Enumerable, and Queryable.

For those of you who want to get a closer look at the real code, here’s one of the queries shown in the demo:

DatabaseProvider provider =
    new DatabaseProvider(Helper.MYSQL_DATA_SOURCE);
provider.emps
    .where(
        new Predicate1<Employee>() {
            public boolean apply(Employee v1) {
                return v1.manager;
        }
    })
    .join(
        provider.depts,
        new Function1<Employee, Integer>() {
            public Integer apply(Employee a0) {
                return a0.deptno;
            }
        },
        new Function1<Department, Integer>() {
            public Integer apply(Department a0) {
                return a0.deptno;
            }
        },
        new Function2<Employee, Department, String>() {
            public String apply(Employee v1,
                                Department v2) {
                return v1.name + " works in " + v2.name;
            }
        }
    )
    .foreach(
        new Function1<String, Void>() {
            public Void apply(String a0) {
                System.out.println(a0);
                return null;
            }
        }
    );

and here is its (not yet implemented) sugared syntax:

List<String> strings =
    from emp in provider.emps
      join dept in provider.depts on emp.deptno == dept.deptno
    where emp.manager
    orderBy emp.name
    select emp.name + " works in " + dept.name;

For more information, visit the linq4j project’s home page.