Linq for Java Tool

What is the Java equivalent for LINQ?

There is nothing like LINQ for Java.

...

Edit

Now with Java 8 we are introduced to the Stream API, this is a similar kind of thing when dealing with collections, but it is not quite the same as Linq.

If it is an ORM you are looking for, like Entity Framework, then you can try Hibernate

:-)

LINQ for Java tool

LINQ for Java would be lovely, but the problem is the language integration.

Java doesn't have anything as concise as lambda expressions, and they're one of the bedrocks of LINQ. I suppose they could layer the query expression support on top of normal Java without lambda expressions, by making the expansion create anonymous inner classes - but it would be pretty hideous. You'd also need expression trees if you wanted to do anything like LINQ to SQL.

Checked exceptions might get in the way, but we'd have to see. The equivalent of IQueryable would need to have some sort of general checked exception - or possibly it could be generic in both the element type and the exception type...

Anyway, this is all pie-in-the-sky - given the troubles the Java community is having with closures, I think it would be folly to expect anything like LINQ in Java itself earlier than about 2012. Of course, that's not to say it wouldn't be possible in a "Java-like" language. Groovy has certain useful aspects already, for instance.

For the library side, Hibernate already provides a "non-integrated" version of a lot of the features of LINQ to SQL. For LINQ to Objects, you should look at the Google Java Collections API - it's a lot of the same kind of thing (filtering, projecting etc). Without lambdas it's a lot fiddlier to use, of course - but it's still really, really handy. (I use the Google Collections code all the time at work, and I'd hate to go back to the "vanilla" Java collections.)

Is there something like LINQ for Java?

Look at Scala, which is powerful functional programming language, but is similar to Java and runs on Java platform.

In Scala it is possible to use essentially the same code constructs as in LINQ, albeit without special query comprehensions syntax present in C# or VB.

EDIT :

Here's an example of Scala's querying capabilities :

// Get all StackOverflow users with more than 20,000 reputation points.
val topUsers = for{
u <- users
if u.reputation > 20000
} yield u;

println ("Users with more than 20,000 reputation:")
for (u <- topUsers) {
println u.name
}

Similar features of LINQ

There are many languages that have syntax or library functions for operating on sequences of objects in a mostly functional way. For example Python has lambda functions, list comprehensions, generators, and the itertools module.

However I don't know of any language that can get anywhere near everything that LINQ can do as cleanly and concisely. Remember that LINQ is not just a way to operate on in-memory structures - LINQ has many providers that share a similar interface:

  • LINQ to Objects
  • LINQ to XML
  • LINQ to SQL
  • etc..

Microsoft have done a good job with LINQ. I am sure some other languages will take inspiration from the success of LINQ and consider how to add similar features in the near future.

Java LINQ -like functionality

Well this is quite difficult to do in java since LINQ is built and integrated into the C# language. If I were to do this, I would probably create a class for all actions in MySQL: for example "Select", "Join"... and then make methods for actions you would like to conduct on them. Select(...).From where "From" is a method on the Select class. This is a very simplified example of course, but what you need to do is create a SQL compiler in Java, and then add any custom methods or classes you would want.

A word of caution however, LINQ is more efficient than writing loops in C#, however if you were to write your own custom Java LINQ, you would simply be writing shortcuts. What I am trying to say is that, you will not be making your code any more efficient, simply more readable and compact.

Can we use Linq for geting value from collection object in java?

Uhm... yes! But you need this library for Java LINQ stuff: //github.com/nicholas22/jpropel-light

How to do it:

import java.util.Hashtable;
import lombok.ExtensionMethod;
import propel.core.utils.Linq;
import java.util.List;
import lombok.Function;

@ExtensionMethod({Linq.class})
public class Main
{

public static void main(String[] args)
{
Hashtable t1=new Hashtable();
t1.put("StdID","A1");
t1.put("RollNo","23");
t1.put("Class","First");
Hashtable t2=new Hashtable();
t2.put("StdID","A2");
t2.put("RollNo","13");
t2.put("Class","First");
Hashtable t3=new Hashtable();
t3.put("StdID","A3");
t3.put("RollNo","53");
t3.put("Class","Second");
Hashtable t4=new Hashtable();
t4.put("StdID","A4");
t4.put("RollNo","33");
t4.put("Class","Third");

Hashtable main = new Hashtable();
main.put(new Integer(1), t1);
main.put(new Integer(2), t2);
main.put(new Integer(3), t3);
main.put(new Integer(4), t4);

List<Hashtable> result= main.values().where(classEquals("First")).toList();
for(Hashtable ht : result)
System.out.println(ht.get("StdID"));
}

@Function
private static Boolean classEquals(Hashtable table, String _class) {
return table.get("Class") != null && table.get("Class").equals(_class);
}
}


Related Topics



Leave a reply



Submit