Java.Java8.Tell us about parallel processing in Java 8 ?

🔹 What Is Parallel Processing?

Parallel processing is a technique that divides a task into subtasks and executes them concurrently using multiple threads (usually across multiple CPU cores).

Java 8 introduced the parallelStream() method that enables parallelism in Streams with very little effort.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.Tell us about parallel processing in Java 8 ?

Java.Java8.What are the flatMap(), flatMapToInt(), flatMapToDouble(), flatMapToLong() methods for in streams?

🔹 flatMap() and its variants

✅ flatMap(Function<T, Stream<R>> mapper)

Used to:

  • Transform each element in the stream into a stream of values
  • Flatten those inner streams into one continuous stream

Example:

List<String> words = List.of("hello", "world");

List<String> letters = words.stream()
    .flatMap(word -> Arrays.stream(word.split("")))
    .collect(Collectors.toList());

System.out.println(letters); // [h, e, l, l, o, w, o, r, l, d]
Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What are the flatMap(), flatMapToInt(), flatMapToDouble(), flatMapToLong() methods for in streams?

Java.Java8.What is the sorted() method for in streams?

Syntax

Stream<T> sorted();
Stream<T> sorted(Comparator<? super T> comparator);

🔹 How It Works

  • sorted() (no arguments):
    Sorts the stream elements in natural order, i.e., using the Comparable interface. This requires the elements to implement Comparable.
  • sorted(Comparator) (with comparator):
    Sorts the stream using a custom Comparator you provide.
Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What is the sorted() method for in streams?

Java.Java8.What is the limit() method for in streams?

The limit() method is a handy tool in the Java Stream API that lets you restrict the number of elements flowing through a stream.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What is the limit() method for in streams?

Java.Java8.What is the purpose of the filter() method in streams?

Great question again, Stanley! 🔥
The filter() method is one of the most commonly used tools in the Stream API, and it’s essential for writing clean, expressive code.

✅ What is the filter() Method?

Stream<T> filter(Predicate<? super T> predicate)

📌 Purpose:

  • To select only the elements that match a condition
  • It uses a Predicate<T> (a function that returns true or false)
  • Returns a new stream with only the matching elements
Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What is the purpose of the filter() method in streams?

Java.Java8.What are the map() and mapToInt(), mapToDouble(), mapToLong() methods used for in streams?

✅ 1. map(Function<T, R>)

🔧 Purpose:

Transforms each element of the stream using a function, returning a new stream of possibly different type.

Stream<String> names = Stream.of("Stan", "Alex");
Stream<Integer> lengths = names.map(String::length);

Input: T

Output: R

Returns: Stream<R>

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What are the map() and mapToInt(), mapToDouble(), mapToLong() methods used for in streams?

Java.Java8.What are the forEach() and forEachOrdered() methods used for in streams?

Let’s break down the difference between forEach() and forEachOrdered() — two commonly used terminal operations in Java Streams.

✅ 1. forEach()

📌 Definition:

void forEach(Consumer<? super T> action)

Applies a given action to each element of the stream

Order is not guaranteed, especially when using parallel streams

🧠 Example:

Stream.of("A", "B", "C")
    .forEach(System.out::print); // Output: ABC (but order is not guaranteed in parallel)

⚠️ In a parallel stream:

Stream.of("A", "B", "C").parallel()
    .forEach(System.out::print); // Output may be: BCA, CAB, etc.
Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What are the forEach() and forEachOrdered() methods used for in streams?

Java.Java8.What is the collect() method in streams?

The .collect() method in Java Streams is one of the most powerful and important terminal operations — especially when you’re turning your processed data into a collection or a result.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What is the collect() method in streams?

Java.Java8.What is the difference between Collection and Stream?

Understanding the difference between Collection and Stream is essential to writing clean, modern Java code.

Let’s break it down in a clear and structured way.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What is the difference between Collection and Stream?

Java.Java8.What are the different ways to create a stream?

Let’s explore all the different ways to create a Stream in Java — one of the most fundamental parts of using the Stream API effectively.

✅ 1. From a Collection (List, Set, etc.)

List<String> list = List.of("apple", "banana", "cherry");
Stream<String> stream = list.stream();
Stream<String> parallelStream = list.parallelStream(); // Runs in parallel

✅ 2. From Arrays

String[] array = {"one", "two", "three"};
Stream<String> stream = Arrays.stream(array);

You can also use primitive array variants:

int[] numbers = {1, 2, 3};
IntStream intStream = Arrays.stream(numbers);
Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What are the different ways to create a stream?