"jLinqer" is a Java implementation of LINQ.
<dependency>
<groupId>com.github.jlinqer</groupId>
<artifactId>jlinqer</artifactId>
<version>1.0.1</version>
</dependency>
The following operations are available.
LINQ(C#) | jLinqer (Java 8) | Stream (Java 8) |
---|---|---|
Where | where | filter |
Select | select | map |
OrderBy | orderBy | sorted |
OrderByDescending | orderByDescending | n/a |
ThenBy | thenBy | n/a |
ThenByDescending | thenByDescending | n/a |
SelectMany | selectMany | flatMap |
Skip | skip | skip |
SkipWhile | skipWhile | n/a |
Take | take | limit |
TakeWhile | takeWhile | n/a |
Concat | concat | concat |
Intersect | intersect | n/a |
Union | union | n/a |
Except | except | n/a |
Join | join | n/a |
GroupJoin | groupJoin | n/a |
Reverse | reverse | n/a |
Zip | zip | n/a |
Distinct | distinct | distinct |
Aggregate | aggregate | reduce |
GroupBy | groupBy | Collectors.groupingBy |
Average | averageXXX | Collectors.summarizingXXX |
Count / LongCount | count | count |
Max | max | max |
Min | min | min |
Sum | sumXXX | Collectors.summarizingXXX |
First | first | findFirst |
FirstOrDefault | firstOrDefault | n/a |
Last | last | n/a |
LastOrDefault | lastOrDefault | n/a |
Single | single | n/a |
SingleOrDefault | singleOrDefault | n/a |
DefaultIfEmpty | defaultIfEmpty | n/a |
ElementAt | elementAt | n/a |
ElementAtOrDefault | elementAtOrDefault | n/a |
All | all | allMatch |
Any | any | anyMatch |
Empty | empty | n/a |
Range | range | n/a |
Repeat | repeat | n/a |
SequenceEqual | sequenceEqual | n/a |
Cast | cast | n/a |
OfType | ofType | n/a |
List<Integer> list = new List<>(1, 2, 3);
List<Integer> actual = list.where(x -> x == 1 || x == 3).toList();
assertEquals(true , actual.contains(1));
assertEquals(false, actual.contains(2));
assertEquals(true , actual.contains(3));
List<Person> list = new List<>(
new Person("React" , 1),
new Person("Angular" , 3),
new Person("Backbone", 5)
);
List<String> actual = list.select(x -> x.name).toList();
assertEquals("React" , actual.get(0));
assertEquals("Angular" , actual.get(1));
assertEquals("Backbone", actual.get(2));
List<String> list = new List<>("Backbone", "Angular", "React");
List<String> actual = list.orderBy(x -> x).toList();
assertEquals("Angular" , actual.get(0));
assertEquals("Backbone", actual.get(1));
assertEquals("React" , actual.get(2));
List<String> list = new List<>("Backbone", "Angular", "React");
List<String> actual = list.orderByDescending(x -> x).toList();
assertEquals("React" , actual.get(0));
assertEquals("Backbone", actual.get(1));
assertEquals("Angular" , actual.get(2));
List<Person> list = new List<>(
new Person("Angular2", 2),
new Person("Angular1", 2),
new Person("React" , 1)
);
List<String> actual = list.orderBy(x -> x.age).thenBy(x -> x.name).toList();
assertEquals("React" , actual.get(0).name);
assertEquals("Angular1", actual.get(1).name);
assertEquals("Angular2" , actual.get(2).name);
List<Person> list = new List<>(
new Person("Angular2", 2),
new Person("Angular1", 2),
new Person("React" , 1)
);
List<String> actual = list.orderBy(x -> x.age).thenByDescending(x -> x.name).toList();
assertEquals("React" , actual.get(0).name);
assertEquals("Angular2", actual.get(1).name);
assertEquals("Angular1" , actual.get(2).name);
List<Person> list = new List<>(
new Person("Angular", 3, new List("1.0.1", "1.0.2")),
new Person("React" , 1, new List("2.0.1", "2.0.2"))
);
List<String> actual = list.selectMany(x -> x.versionHistory).toList();
assertEquals("1.0.1", actual.get(0));
assertEquals("1.0.2", actual.get(1));
assertEquals("2.0.1", actual.get(2));
assertEquals("2.0.2", actual.get(3));
List<Integer> list = new List<>(1, 2, 3);
List<Integer> actual = list.skip(2).toList();
assertEquals(3, actual.get(0).intValue());
List<Integer> list = new List<>(1, 2, 3, 4, 5);
List<Integer> actual = list.skipWhile(x -> x <= 3).toList();
assertEquals(4, actual.get(0).intValue());
assertEquals(5, actual.get(1).intValue());
List<String> list = new List<>("Backbone", "Angular", "React");
List<String> actual = list.take(2).toList();
assertEquals(2, actual.size());
assertEquals("Backbone", actual.get(0));
assertEquals("Angular" , actual.get(1));
List<String> list = new List<>("Backbone", "Angular", "React");
List<String> actual = list.takeWhile(x -> x.equals("Backbone") || x.equals("Angular")).toList();
assertEquals(2, actual.size());
assertEquals("Backbone", actual.get(0));
assertEquals("Angular" , actual.get(1));
List<Integer> first = new List<>(1, 2);
List<Integer> second = new List<>(2, 3);
List<Integer> actual = first.concat(second).toList();
assertEquals(1, actual.get(0).intValue());
assertEquals(2, actual.get(1).intValue());
assertEquals(2, actual.get(2).intValue());
assertEquals(3, actual.get(3).intValue());
List<Integer> first = new List<>(1, 2, 3);
List<Integer> second = new List<>(1, 3);
List<Integer> actual = first.intersect(second).toList();
assertEquals(1, actual.get(0).intValue());
assertEquals(3, actual.get(1).intValue());
List<Integer> first = new List<>(1, 2, 3);
List<Integer> second = new List<>(0, 1, 3, 4);
List<Integer> actual = first.union(second).toList();
assertEquals(5, actual.size());
assertEquals(1, actual.get(0).intValue());
assertEquals(2, actual.get(1).intValue());
assertEquals(3, actual.get(2).intValue());
assertEquals(0, actual.get(3).intValue());
assertEquals(4, actual.get(4).intValue());
List<Integer> first = new List<>(1, 2, 3);
List<Integer> second = new List<>(1, 3);
List<Integer> actual = first.except(second).toList();
assertEquals(2, actual.get(0).intValue());
List<Javascript> outer = new List<>(
new Javascript("Angular", 1),
new Javascript("React" , 4),
new Javascript("ES2016" , 5)
);
List<Javascript> inner = new List<>(
new Javascript("Angular", 2),
new Javascript("Angular", 3),
new Javascript("ES2016" , 6),
new Javascript("ES7" , 7)
);
Function<Javascript, String> outerKey = (x) -> x.name;
Function<Javascript, String> innerKey = (y) -> y.name;
BiFunction<Javascript, Javascript, Javascript> selector = (x, y) -> new Javascript(x.name, y.age);
List<Javascript> actual = outer.join(inner, outerKey, innerKey, selector).toList();
assertEquals(3, actual.size());
assertEquals("Angular", actual.get(0).name);
assertEquals("Angular", actual.get(1).name);
assertEquals("ES2016" , actual.get(2).name);
assertEquals(2, actual.get(0).age);
assertEquals(3, actual.get(1).age);
assertEquals(6, actual.get(2).age);
List<Javascript> outer = new List<>(
new Javascript("Angular", 1),
new Javascript("React" , 4),
new Javascript("ES2016" , 5)
);
List<Javascript> inner = new List<>(
new Javascript("Angular", 2),
new Javascript("Angular", 3),
new Javascript("ES2016" , 6),
new Javascript("ES7" , 7)
);
Function<Javascript, String> outerKey = (x) -> x.name;
Function<Javascript, String> innerKey = (y) -> y.name;
BiFunction<Javascript, IEnumerable<Javascript>, Javascript> selector = (x, y) -> new Javascript(x.name, y.select(z -> z.age));
List<Javascript> actual = outer.groupJoin(inner, outerKey, innerKey, selector).toList();
assertEquals(3, actual.size());
assertEquals("Angular", actual.get(0).name);
assertEquals("React" , actual.get(1).name);
assertEquals("ES2016" , actual.get(2).name);
assertEquals(2, actual.get(0).ages.elementAt(0));
assertEquals(3, actual.get(0).ages.elementAt(1));
assertEquals(0, actual.get(1).ages.count());
assertEquals(6, actual.get(2).ages.elementAt(0));
List<Integer> list = new List<>(1, 2, 3);
List<Integer> actual = list.reverse().toList();
assertEquals(3, actual.get(0).intValue());
assertEquals(2, actual.get(1).intValue());
assertEquals(1, actual.get(2).intValue());
List<Integer> first = new List<>(1, 2, 3);
List<String> second = new List<>("Angular", "React", "Backbone");
List<Integer> actual = first.zip(second, (x, y) -> String.format("%s %d", x, y)).toList();
assertEquals("1 Angular" , actual.get(0));
assertEquals("2 React" , actual.get(1));
assertEquals("3 Backbone", actual.get(2));
List<Integer> list =
new List<>(
1, 2, 3,
1, 2, 3, 4
);
List<Integer> actual = list.distinct().toList();
assertEquals(1, actual.get(0).intValue());
assertEquals(2, actual.get(1).intValue());
assertEquals(3, actual.get(2).intValue());
assertEquals(4, actual.get(3).intValue());
List<Integer> list = new List<>(1, 2, 3);
int actual = list.aggregate((sum, elem) -> sum + elem);
assertEquals(6, actual);
List<Person> list = new List<>(
new Person("React" , 1),
new Person("Angular" , 1),
new Person("Backbone", 5)
);
Map<Integer, IEnumerable<Person>> actual = list.groupBy(x -> x.age);
assertEquals(true, actual.get(1).any(x -> x.name.equals("React")));
assertEquals(true, actual.get(1).any(x -> x.name.equals("Angular")));
assertEquals(true, actual.get(5).any(x -> x.name.equals("Backbone")));
List<Long> listLong = new List<>(1l, 2l, 3l, 4l);
double actualLong = listLong.averageLong(x -> x);
assertEquals(2.5d, actualLong, 0);
List<String> list = new List<>("Backbone", "Angular", "React");
long actual = list.longCount();
int actualNone = list.count(x -> x.equals("jquery"));
assertEquals(3, actual);
assertEquals(0, actualNone);
List<Double> listDouble = new List<>(1d, 2d, 3d);
double actualDouble = listDouble.max(x -> x);
assertEquals(3d, actualDouble, 0);
List<BigDecimal> listBigDecimal = new List<>(
new BigDecimal(1d),
new BigDecimal(2d),
new BigDecimal(3d)
);
BigDecimal actualBigDecimal = listBigDecimal.min(x -> x);
assertEquals(1d, actualBigDecimal.doubleValue(), 0);
List<Integer> listInt = new List<>(1, 2, 3);
int actualInt = listInt.sumInt(x -> x);
assertEquals(6, actualInt);
List<String> list = new List<>("Backbone", "Angular", "React");
String actualFirst = list.firstOrDefault();
String actualMatch = list.firstOrDefault(x -> x.equals("Angular"));
String actualUnMatch = list.firstOrDefault(x -> x.equals("jquery"));
assertEquals("Backbone", actualFirst);
assertEquals("Angular" , actualMatch);
assertEquals(null , actualUnMatch);
List<Integer> list = new List<>(1, 2, 3);
List<Integer> listEmpty = new List<>();
int actual = list.lastOrDefault();
Integer actualDefaultNone = listEmpty.lastOrDefault(x -> x == 0);
assertEquals(3, actual);
assertEquals(null, actualDefaultNone);
List<Integer> listMany = new List<>(1, 2, 3);
List<Integer> listEmpty = new List<>();
int actualFilter = listMany.singleOrDefault(x -> x == 3);
Integer actualUnMatch = listEmpty.singleOrDefault(x -> x == 0);
assertEquals(3, actualFilter);
assertEquals(null, actualUnMatch);
List<String> listEmpty = new List<>();
List<String> actualDefault = listEmpty.defaultIfEmpty("ES7").toList();
assertEquals("ES7", actualDefault.get(0));
List<Integer> list = new List<>(1, 2, 3);
int actual = list.elementAtOrDefault(2);
Integer actualDefault = list.elementAtOrDefault(3);
assertEquals(3, actual);
assertEquals(null, actualDefault);
List<String> list = new List<>("Backbone", "Angular", "React");
boolean actual = list.all(x -> x.equals("Angular") || x.equals("Backbone") || x.equals("React"));
boolean actualNotFound = list.all(x -> x.equals("Angular") || x.equals("React"));
assertEquals(true, actual);
assertEquals(false, actualNotFound);
List<String> list = new List<>("Backbone", "Angular", "React");
boolean actual = list.any(x -> x.equals("Angular"));
boolean actualNotFound = list.any(x -> x.equals("jquery"));
assertEquals(true, actual);
assertEquals(false, actualNotFound);
List<Double> actual = IEnumerable.empty(Double.class);
assertEquals(0, actual.count());
List<Integer> actual = IEnumerable.range(-2, 3);
assertEquals(-2, actual.get(0).intValue());
assertEquals(-1, actual.get(1).intValue());
assertEquals(0 , actual.get(2).intValue());
List<String> actual = IEnumerable.repeat(String.class, "Law of Cycles", 10);
assertEquals(10, actual.count());
assertEquals("Law of Cycles", actual.get(9));
List<Integer> first = new List<>(1, 2, 3);
List<Integer> secondMatch = new List<>(1, 2, 3);
List<Integer> secondUnMatchElem = new List<>(1, 2, 4);
boolean actualMatch = first.sequenceEqual(secondMatch);
boolean actualUnMatchElm = first.sequenceEqual(secondUnMatchElem);
assertEquals(true, actualMatch);
assertEquals(false, actualUnMatchElm);
List<Object> list = new List<>(1, 2, 3);
List<Integer> actual = list.cast(Integer.class).toList();
assertEquals(1, actual.get(0).intValue());
assertEquals(2, actual.get(1).intValue());
assertEquals(3, actual.get(2).intValue());
List<Object> list = new List<>(1, "2", 3, "4");
List<String> actualStr = list.ofType(String.class).toList();
List<Integer> actualInt = list.ofType(Integer.class).toList();
assertEquals("2", actualStr.get(0));
assertEquals("4", actualStr.get(1));
assertEquals(1 , actualInt.get(0).intValue());
assertEquals(3 , actualInt.get(1).intValue());
- Microsoft Reference Source, "Enumerable.cs", http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs
- GitHub, "javaLinq", https://github.com/sircodesalotOfTheRound/javaLinq
- Qiita, "LINQ to Objects と Java8-Stream API の対応表", http://qiita.com/amay077/items/9d2941283c4a5f61f302
- stackoverflow, "Generic method to perform a map-reduce operation. (Java-8)", http://stackoverflow.com/questions/30826674/generic-method-to-perform-a-map-reduce-operation-java-8