NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io


What is a Stream?
Introduced in Java, 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.

You can use Stream to filter, collect, print, and convert from one data structure to another.
Stream does not store elements; it conveys elements from a source (e.g., a data structure, an array, or an I/O channel) through a pipeline of computational operations.
Stream operations are functional in nature and do not modify the source collection.

Explain Stream Operations:
Stream operations are divided into intermediate and term op.
Intermediate operations transform a stream into another stream (e.g., filter (if), sorted, element transformation).

Example:
productsList.stream()
.filter(product -> product.getPrice() == 30000)
.forEach(product -> System.out.println(product.getPrice()));

Term op produce a result (e.g., forEach, min, max, count, sum, or a new collection).
Example: Using forEach() to print elements:



What are the differences between map() and flatMap()?
map() transforms each element into another element (e.g., converting a list of strings to uppercase).
flatMap() flattens nested streams (e.g., when dealing with lists of lists).


Explain the difference between findFirst() and findAny():
findFirst() returns the first element in the stream (useful for sequential streams).
findAny() returns any element in the stream (useful for parallel streams).


What is Lazy Evaluation?
Lazy evaluation refers to the delayed execution of op on a stream until the result is actually needed.
When you perform multiple stream operations (such as filtering, mapping, or sorting), the intermediate operations are not executed immediately. Instead, they are deferred until a term op is invoked.
This deferred execution allows the Stream API to optimize performance by avoiding unnecessary computations.


Sequential Stream:
A sequential stream processes its elements one by one, using a single thread.
By default, any stream operation in Java is processed sequentially unless explicitly specified as parallel.
Sequential streams are like a for loop that operates on a single core.
They do not take advantage of multi-core systems, even if the underlying system supports parallel execution.
Example:
Java

List<String> list = Arrays.asList("Hello", "G", "E", "E", "K", "S!");
list.stream().forEach(System.out::print);
// Output: Hello GEEKS!

Parallel Stream:
A parallel stream divides a task into multiple sub-tasks and runs them in different threads, utilizing multiple cores (if available).
Parallel streams are useful for leveraging multi-core processors, which can significantly improve performance.
The order of execution is not guaranteed, and results may be unordered.
Example:
Java

List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
numbers.parallelStream().forEach(number -> System.out.print(number + " "));
// Output (order may vary): 1 2 3 4

When to Use Each:
Use sequential streams when you want predictable order and simple processing.
Use parallel streams when you have computationally intensive tasks that can benefit from parallel execution.
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.