Breaking the silo #2

Linq VS Stream

Java 

> Stream extends Java Util library in Java 8

 

> Defined as

"Classes to support functional-style operations on streams of elements, such as map-reduce transformations on collections"

C#

> Linq (Language-Integrated Query) was introduced in C# 3.5

 

> Defined as a

"[...] set of features that extends powerful query capabilities to the language syntax of C#"

(Java 8)

(C# 6)

SQL like query (query syntax)

var query = from Student student in arrList
            where student.Scores[0] > 95
            orderby student.Scores[0]
            select student;
var query = arrList.Where(x => x.Scores[0] > 95)
                   .OrderBy(x => x.Scores[0]);
                

Lambda expression query (method syntax)

C# Linq

Basic Operations

> Simple select

> Filtering

> Ordering

> Grouping

Linq provides extension methods for collections

P-Linq is a parallel implementation of Linq

Java Stream

Stream Computation Pipeline

int sum = widgets.stream()
                      .filter(w -> w.getColor() == RED)
                      .mapToInt(w -> w.getWeight())
                      .sum();
 

A Source

Intermediate Operations

A Terminal Operation

2 ways of getting a Stream

> Through a collection: coll.stream()

> Through the static methods on stream classes: Stream.Of(coll) 

Supports Parallel streams for operations on multiple threads

Stream.of("a1", "a2", "a3")
    .map(s -> s.substring(1))
    .mapToInt(Integer::parseInt)
    .max()
List<Person> filtered = persons.stream()
                                .filter(p -> p.name.startsWith("P"))
                                .collect(Collectors.toList());

List<Person> filtered = persons.Where(p => p.Name.StartsWith("P"))
                                            .ToList();

Java

C#

new[]{"a1", "a2", "a3"}.Select(s => int.Parse(s.Substring(1)))
                       .Max()

Java

C#

Breaking the Java C# Silo #2

By hkoundi

Breaking the Java C# Silo #2

  • 818