user3309769
user3309769

Reputation: 11

Sample maven project (Console) using linq4j

I am new to the java development and would like to use the LINQ equivalent in Java. Searched many websites and found linq4j seems good. I tried the example programs using the maven dependency provided. But am getting the Depenency resolving error as

'Failure to find net.hydromatic:linq4j:jar:0.1.13'.

Could any one help me, how can I resolve this error and use linq4j.

Providing a step by step (Along with pom changes) example will be more useful.

Thanks in Advance,

Kiran

Upvotes: 1

Views: 468

Answers (1)

Julian Hyde
Julian Hyde

Reputation: 1354

Linq4j is available pre-built in the http://conjars.org maven repository. Add the following sections to your pom.xml:

  <dependencies>
    ...
    <dependency>
      <groupId>net.hydromatic</groupId>
      <artifactId>linq4j</artifactId>
      <version>0.1.13</version>
    </dependency>
  </dependencies>

and

  <repositories>
    ...
    <repository>
      <releases>
    <enabled>true</enabled>
        <updatePolicy>always</updatePolicy>
        <checksumPolicy>warn</checksumPolicy>
      </releases>
      <id>conjars</id>
      <name>Conjars</name>
      <url>http://conjars.org/repo</url>
      <layout>default</layout>
    </repository>
  </repositories>

Then you should be able to use linq4j classes from your Java application. For example:

package com.example;

import net.hydromatic.linq4j.Linq4j;
import net.hydromatic.linq4j.function.*;

public class Linq4jExample {
  public static class Employee {
    public final int empno;
    public final String name;
    public final int deptno;

    public Employee(int empno, String name, int deptno) {
      this.empno = empno;
      this.name = name;
      this.deptno = deptno;
    }

    public String toString() {
      return "Employee(name: " + name + ", deptno:" + deptno + ")";
    }
  }

  public static final Employee[] emps = {
      new Employee(100, "Fred", 10),
      new Employee(110, "Bill", 30),
      new Employee(120, "Eric", 10),
      new Employee(130, "Janet", 10),
  };

  public static final Function1<Employee, Integer> EMP_DEPTNO_SELECTOR =
      new Function1<Employee, Integer>() {
        public Integer apply(Employee employee) {
          return employee.deptno;
        }
      };

  public static void main(String[] args) {
    String s = Linq4j.asEnumerable(emps)
        .groupBy(
            EMP_DEPTNO_SELECTOR,
            new Function0<String>() {
              public String apply() {
                return null;
              }
            },
            new Function2<String, Employee, String>() {
              public String apply(String v1, Employee e0) {
                return v1 == null ? e0.name : (v1 + "+" + e0.name);
              }
            },
            new Function2<Integer, String, String>() {
              public String apply(Integer v1, String v2) {
                return v1 + ": " + v2;
              }
            })
        .orderBy(Functions.<String>identitySelector())
        .toList()
        .toString();
    assert s.equals("[10: Fred+Eric+Janet, 30: Bill]");
  }
}

Upvotes: 1

Related Questions