5th Unit 2 Mark in Java
5th Unit 2 Mark in Java
```Example
(a, b) -> a + b
```
4. **Iterating Over a List Using Lambda Expressions** You can iterate over a list and
display its elements using lambda expressions as follows: Example :
5. **Starting a Thread in Java Using Lambda Expression** To start a thread using a lambda
expression, you can implement the `Runnable` interface like this:
```Example
Thread thread = new Thread(() -> System.out.println("Thread is running"));
thread.start(); ```
```Example
Comparator<String> comparator = (s1, s2) -> s1.length() - s2.length();
Arrays.sort(arrayOfStrings, comparator);
```
8. **How the sort() Method of Collections Class Works** The `sort()` method in the
`Collections` class sorts elements in a list based on their natural ordering or according to
a specified comparator. It modifies the list in place and uses a stable sorting algorithm,
ensuring that equal elements maintain their relative order.
12. The purpose of the **reduce() method** in collections is to aggregate values into a single
result by applying a binary operator repeatedly. It combines elements using an associative
accumulation function and can return an Optional describing the reduced value if any[2]
[4].
13. The syntax for converting a **List into Set** in Java Stream is:
`
Example
Set<Type> set = list.stream().collect(Collectors.toSet());
```
This utilizes the `Collectors.toSet()` method to collect the stream elements into a Set[6][7].
14. The syntax for converting a **List into Map** in Java Stream is:
Example
Map<KeyType, ValueType> map =
list.stream().collect(Collectors.toMap(Function.identity(), /* value mapper
*/));
``` Here, you specify how to derive keys and values from the list elements[6][7].
16. The **map() function** transforms each element of the stream using a provided function,
producing a new stream of transformed elements. It is used to apply operations like
conversions or modifications on each element in the pipeline[3][5].
- **Synchronous Execution**: In this model, tasks are executed sequentially, where each
task must complete before the next one begins. This can lead to delays if a task takes longer
than expected.
- **Observable**: Represents a stream of data that can be observed and reacted to.
- **Schedulers**: Manage threading and execution context for Observables and Observers,
allowing for efficient execution.
- **Operators**: Functions that transform, filter, or combine Observables for more complex
data manipulation.
- **Subjects**: Special types of Observables that allow manual emission of items while also
acting as an Observer, facilitating bidirectional communication.