Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quaere - Anyone using it yet? (LINQ to Objects for Java) [closed]

I'm a .NET guy originally, working in Java recently, and finding I'm really missing LINQ to Objects, specifically for performing filtering against collections.

A few people here on Stack Overflow have answered the "LINQ for Java?" question with a single word :

Quaere

However, on the site it clearly states "Pre-Beta", and there's been no commits to their code for over a year, so I'm guessing the project is pretty much dead.

Is anyone actually using this, and / or have any experience with it?

The second most common answer appears to be "use Google Collections". Is this the most appropriate Java way?

Cheers

Marty

like image 990
Marty Pitt Avatar asked Jul 08 '09 07:07

Marty Pitt


People also ask

Is there an equivalent to LINQ in Java?

In . NET, an easy way for you to simplify querying datasets is LINQ. Java doesn't have this, but since the introduction of Java 8 in 2014, you do now have the possibility to use "streams". Both LINQ and streams are ways to simplify querying datasets and to provide developers with an easy API to use.

What are the uses of LINQ to objects?

The term "LINQ to Objects" refers to the use of LINQ queries with any IEnumerable or IEnumerable<T> collection directly, without the use of an intermediate LINQ provider or API such as LINQ to SQL or LINQ to XML. You can use LINQ to query any enumerable collections such as List<T>, Array, or Dictionary<TKey,TValue>.

Is LINQ good to use?

Advantages of LINQStandardized way of querying multiple data sources: The same LINQ syntax can be used to query multiple data sources. Compile time safety of queries: It provides type checking of objects at compile time. IntelliSense Support: LINQ provides IntelliSense for generic collections.

What is LINQ in which scenarios it should be used?

LINQ is a data querying API that provides querying capabilities to . NET languages with a syntax similar to a SQL. LINQ queries use C# collections to return data. LINQ in C# is used to work with data access from sources such as objects, data sets, SQL Server, and XML. LINQ stands for Language Integrated Query.


3 Answers

You can select the items in a collection (and much more) in a more readable way by using the lambdaj library

http://code.google.com/p/lambdaj/

like image 53
Mario Fusco Avatar answered Oct 20 '22 22:10

Mario Fusco


Quaere is in a pioneer position with LINQ in Java, but is not typesafe, which is one of the main points of LINQ.

Querydsl is type-safe and supports filtering, sorting and projecting Collections.

It supports operations on JPA/Hibernate, JDO and SQL backends as well.

The syntax is similar to SQL with the difference that the basic order is from-where-list.

I am the maintainer of Querydsl, so this answer is biased.

like image 22
Timo Westkämper Avatar answered Oct 20 '22 23:10

Timo Westkämper


For simple Linq To Objects, the best I think can be done in Java is something like this:

Vector<Integer> numbers = new Vector<Integer>();

numbers.add(42);
numbers.add(3);
numbers.add(16);
numbers.add(92);
numbers.add(9);

Iterable<Integer> filtered = new Where<Integer>(numbers) {
    protected boolean predicate(Integer i) { return i > 10; }
};

Iterable<String> converted = new Select<Integer, String>(filtered) {
    protected String select(Integer i) { return i.toString(); }
};

for (final String str : converted)
    System.out.println(str);

Note that I haven't got Where and Select chaining together in one expression. I could insert the definition of filtered into the one place it's used, but that would probably make it (even) less readable. The problems are the lack of extension methods and lambdas. The closest we can get to lambdas is by these anonymous class declarations. They can refer to objects named in the enclosing scope, but only finals, so they cannot mutate anything (unlike lambdas in C#).

Also the hugely verbose syntax is a pain. People have often proposed that Java should offer a simpler syntax for cases where there is only one abstract (or interface) method, and hence there is no need to give the name or type declarations for what you want to override. Then there's the fact that there's no type inference, and no obvious way to provide it on generic class constructors because new Select(filtered) already means something else.

The implementations for Select and Where are:

abstract class Select<TSource, TResult> implements Iterable<TResult>
{
    private Iterable<TSource> _source;

    public Select(Iterable<TSource> source)
        { _source = source; }

    private class Iter implements Iterator<TResult>
    {
        private Iterator<TSource> _i;

        public Iter() { _i = _source.iterator(); }

        public void remove()
            { _i.remove(); }

        public boolean hasNext()
            { return _i.hasNext(); }

        public TResult next()
            { return select(_i.next()); }
    }

    protected abstract TResult select(TSource source);

    public Iterator<TResult> iterator()
        { return new Iter(); }
}

abstract class Where<TSource> implements Iterable<TSource>
{
    private Iterable<TSource> _source;

    public Where(Iterable<TSource> source)
        { _source = source; }

    private class Iter implements Iterator<TSource>
    {
        private Iterator<TSource> _i;
        private TSource _cachedNext;
        private boolean _hasCachedNext;

        public Iter()
        {
            _i = _source.iterator();
            fetch();
        }

        public void remove()
            { _i.remove(); }

        public boolean hasNext()
            { return _hasCachedNext; }

        public TSource next()
        {
            TSource result = _cachedNext;
            fetch();
            return result;
        }

        private void fetch()
        {
            _hasCachedNext = false;

            while (_i.hasNext())
            {
                _cachedNext = _i.next();
                if (predicate(_cachedNext))
                {
                    _hasCachedNext = true;
                    return;
                }
            }
        }
    }

    protected abstract boolean predicate(TSource source);

    public Iterator<TSource> iterator()
        { return new Iter(); }
}
like image 1
Daniel Earwicker Avatar answered Oct 20 '22 23:10

Daniel Earwicker