Java.Java8.What are the functional interfaces Function, DoubleFunction, IntFunction, and LongFunction?

Let’s break down Function<T, R> and DoubleFunction<R> — they’re both part of the java.util.function package and are used to represent functions as objects in Java.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What are the functional interfaces Function, DoubleFunction, IntFunction, and LongFunction?

Java.Java8.What are functional interfaces?

Let’s dive in:


✅ What is a Functional Interface?

A functional interface is an interface that has exactly one abstract method (but can have default or static methods too).

It’s the backbone of lambda expressions and method references in Java.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What are functional interfaces?

Java.Java8.Explain the System.out::println expression.

The expression System.out::println is a method reference, and specifically it’s:

🔹 Type: Reference to an instance method of a particular object (Type #2)

Let’s break it down:

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.Explain the System.out::println expression.

Java.Java8.What types of method references do you know?

In Java, method references are a shorthand syntax for calling methods via lambda expressions. There are four main types of method references:


✅ 1. Reference to a static method

Syntax: ClassName::staticMethodName

Example:

Function<String, Integer> parser = Integer::parseInt;
System.out.println(parser.apply("123")); // Output: 123
Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What types of method references do you know?

Java.Java8.What is a method reference?

🔎 What is a Method Reference?

A method reference is a compact, readable way to refer to an existing method by name rather than writing a full lambda.

It’s basically a lambda that just calls an existing method.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What is a method reference?

Java.Java8.How can I sort a list of strings using a lambda expression?

✅ Basic Example – Sort Strings Alphabetically

List<String> names = Arrays.asList("Charlie", "Alice", "Bob");

names.sort((s1, s2) -> s1.compareTo(s2));  // ascending order
System.out.println(names); // [Alice, Bob, Charlie]

Java’s List.sort() method expects a Comparator, and the lambda provides the comparator logic inline.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.How can I sort a list of strings using a lambda expression?

Java.Java8.What variables can lambda expressions access?

Lambdas can access variables from three main places:

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What variables can lambda expressions access?

Java.Java8.What is a lambda? What is the structure and usage features of a lambda expression?

Let’s break down what a lambda expression is in Java, its structure, and its usage features.


🔹 What is a Lambda Expression?

A lambda expression is a concise way to represent an anonymous function (a function without a name) that can be passed around as a value. It allows you to treat code as data.

🧠 Think of a lambda as a shortcut for writing instances of functional interfaces.

Lambda expressions were introduced in Java 8 to support functional programming and work seamlessly with features like the Streams API.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What is a lambda? What is the structure and usage features of a lambda expression?

Java.Java8.What are the new features in Java 8 and JDK 8?

🔹 1. Lambda Expressions

Lambda expressions enable you to treat functionality as a method argument or pass code as data. They simplify the development of APIs that rely on passing behavior.

List<String> list = Arrays.asList("a", "b", "c");
list.forEach(s -> System.out.println(s));
Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What are the new features in Java 8 and JDK 8?

Java.Collections.Make a HashMap from a HashSet>.

✅ If the HashSet comes from an existing Map.entrySet():

Map<K, V> map = new HashMap<>(Map.ofEntries(...)); // Java 9+

But more generally, if you already have a Set<Map.Entry<K, V>> (like a HashSet), here’s the clear and compatible way:

Continue reading →
Posted in Без рубрики | Comments Off on Java.Collections.Make a HashMap from a HashSet>.