Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Java 8 Streams the same as .Net IEnumerable? [closed]

Tags:

java

.net

java-8

At first I thought Java Streams were necessarily related to I/O but they look really like the IEnumerable interface in .Net.

Is that comparison fair?

like image 682
Otávio Décio Avatar asked Feb 14 '14 14:02

Otávio Décio


People also ask

What are two types of streams in Java 8?

With Java 8, Collection interface has two methods to generate a Stream. stream() − Returns a sequential stream considering collection as its source. parallelStream() − Returns a parallel Stream considering collection as its source.

Do Java streams need to be closed?

Streams have a BaseStream. close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files. lines(Path, Charset)) will require closing.

What are streams in Java 8?

Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.

Is there an equivalent to LINQ in Java?

There is nothing like LINQ for Java. 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.


1 Answers

Maybe this is something interesting I found on google for you:

Streams

Java streams (not to be confused with InputStream and OutputStream) do more or less the same thing as LINQ, with parallel processing mirroring PLINQ. There isn't any nice SQL-like syntax to use, though - you have to do it function-style. And just as LINQ required extension methods, streams don't appear until Java 8 because they need defender methods to work with existing collection types.

Stream is largely equivalent to .NET IEnumerable. To see how they're similar, consider these examples:

// Write each value in a collection to standard output on a separate line:  // C# - LINQ myCollection.ForEach( x => Console.WriteLine(x) ); // Java - stream myCollection.stream().forEach( x -> System.out.println(x) );  // Sum all the values in a (potentially large) collection, using parallelism // if possible:  // C# - PLINQ int sum = myCollection.AsParallel().Aggregate( (x, y) => x + y ); // Java - parallel stream int sum = myCollection.stream().parallel().reduce( (x, y) -> x + y ); 

You would expect the stream() method to be on Iterable, in the same way as LINQ operates on IEnumerable, but it's on Collection instead. Perhaps it's because Java lacks yield-return semantics, so Iterable is just less interesting or useful in Java.

source: http://leftoblique.net/wp/2013/07/25/java-8-a-k-a-oracle-finally-catches-up-to-net-framework-3-0/

EDIT: There is a lot to find about it on Google. Here are some more interesting articles: https://web.archive.org/web/20130331002411/http://blog.informatech.cr/2013/03/24/java-streams-preview-vs-net-linq/

like image 147
P Griep Avatar answered Sep 22 '22 01:09

P Griep