What Is the Java Equivalent for Linq

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

:-)

Java equivalent of Where Clause in C# Linq

Java 8 introduces the Stream API that allows similar constructs to those in Linq.

Your query for example, could be expressed:

int cheetahNumber = 77;

Animal cheetah = animals.stream()
.filter((animal) -> animal.getNumber() == cheetahNumber)
.findFirst()
.orElse(Animal.DEFAULT);

You'll obviously need to workout if a default exists, which seems odd in this case, but I've shown it because that's what the code in your question does.

Equivalent of LINQ in Java 8

Java's Stream API is the closest thing to .NET's LINQ, in the sense that it allows you to query/manipulate collections in a functional style.

Is there a Java equivalent for LINQ?

This library provides a full LINQ API: https://github.com/nicholas22/jpropel-light

It does so with functional-style constructs and it also uses deferred execution.

// select names starting with j, using LINQ-style statements
new String[] { "james", "john", "john", "eddie" }.where(startsWith("j")).distinct().all(println());

What is the Java 8 Stream API equivalent for LINQ Join?

I haven't found any existing equivalent, but the below method should work:

public static <Outer, Inner, Key, Result> Stream<Result> join(
Stream<Outer> outer, Stream<Inner> inner,
Function<Outer, Key> outerKeyFunc,
Function<Inner, Key> innerKeyFunc,
BiFunction<Outer, Inner, Result> resultFunc) {

//Collect the Inner values into a list as we'll need them repeatedly
List<Inner> innerList = inner.collect(Collectors.toList());

//matches will store the matches between inner and outer
final Map<Outer, List<Inner>> matches = new HashMap<>();

//results will be used to collect the results in
final List<Result> results = new ArrayList<>();

outer.forEach(o -> innerList
.stream()
//Filter to get those Inners for which the Key equals the Key of this Outer
.filter(i -> innerKeyFunc.apply(i).equals(outerKeyFunc.apply(o)))
.forEach(i -> {
if (matches.containsKey(o)) {
//This Outer already had matches, so add this Inner to the List
matches.get(o).add(i);
} else {
//This is the first Inner to match this Outer, so create a List
List<Inner> list = new ArrayList<>();
list.add(i);
matches.put(o, list);
}
}));

matches.forEach((out, in) -> in.stream()
//Map each (Outer, Inner) pair to the appropriate Result...
.map(i -> resultFunc.apply(out, i))
//...and collect them
.forEach(res -> results.add(res)));

//Return the result as a Stream, like the .NET method does (IEnumerable)
return results.stream();
}

I only did a brief test of the code using the following inputs:

public static void main(String[] args) {
Stream<String> strings = Arrays.asList("a", "b", "c", "e", "f", "d").stream();
Stream<Integer> ints = Arrays.asList(1, 2, 3, 6, 5, 4).stream();
Stream<String> results = join(strings, ints,
Function.identity(),
str -> Integer.parseInt(str, 16) - 9,
(o, i) -> "Outer: " + o + ", Inner: " + i);
results.forEach(r -> System.out.println(r));
}
  • The ints are their own keys, so no transformation
  • The Strings are mapped to ints according to their hex value - 9
  • (The elements match if the int values are equal, as per default)
  • Matching pairs are put into a String

The following (correct) results are printed:

Outer: a, Inner: 1
Outer: b, Inner: 2
Outer: c, Inner: 3
Outer: d, Inner: 4
Outer: e, Inner: 5
Outer: f, Inner: 6

More in-depth testing will be needed, of course, but I believe this implementation to be correct. It could probably be more efficient as well, I'm open to suggestions.

What is the Java equivalent for Enumerable.Select with lambdas in C#?

If you have a list of Persons like List<Person> persons; you can say

List<String> names
=persons.stream().map(x->x.getName()).collect(Collectors.toList());

or, alternatively

List<String> names
=persons.stream().map(Person::getName).collect(Collectors.toList());

But collecting into a List or other Collection is intented to be used with legacy APIs only where you need such a Collection. Otherwise you would proceed using the stream’s operations as you can do everything you could do with a Collection and a lot more without the need for an intermediate storage of the Strings, e.g.

persons.stream().map(Person::getName).forEach(System.out::println);

Java/Android best equivalent for C# linq

In Java8:

Arrays.stream(numbers).filter(n -> n < 3 || n > 7).count();

In response to your requirement for android, check out lambdaj: https://code.google.com/p/lambdaj/ It is compatible for android - although the syntax is different. You'll have to look at the documentation as i've never used it. You could also check out https://github.com/wagnerandrade/coollection

OrderByDecending(LINQ) equivalent in Java

List<Coffee> sorted = lst.stream()
.sorted(Comparator.comparing(s -> !s.trim().equalsIgnoreCase(sender.trim())))
.collect(Collectors.toList());


Related Topics



Leave a reply



Submit