Welcome to the Java 8 Transition: From Functional Style to Imperative Paradigm repository! In this repository, we embark on a journey to explore the transition from functional programming style to the imperative paradigm in Java 8.
Java 8 introduced significant changes to the language, most notably the inclusion of lambda expressions, functional interfaces, and streams. These features empowered developers to embrace functional programming principles and write concise, expressive, and highly modular code.
However, while functional programming offers numerous benefits, there are scenarios where the imperative style might be more suitable or preferred. This repository focuses on understanding and converting functional-style code into imperative code where it aligns better with the problem domain or improves readability and maintainability.
By examining real-world examples and common functional programming patterns, we will explore how to transform code snippets to follow an imperative approach while leveraging the power of Java 8 features. We will discuss scenarios where mutable state, control flow, and explicit iteration are more suitable alternatives to functional constructs.
- Learn Java 8 By Converting Functional Style Code Into Imperative
- Before Start
- Quick Look
- Filter is Logically Equal to If Condition
- The Map is Logically Equal to get add to the resultant Collection
- Some observations while working with Collectors.groupingBy()
- Some observations while working with Collectors.toMap(Function , Function )
- Sort Collection
- Any Match
- Find First with default Value
- Collectors.joining(delimiter)
- Count
- takeWhile
- Notes / Observation
- Collectors.GroupingBy(Function<T,U> function) we can achieve this using Map's computeIfAbsent() method
- Collectors.GroupingBy(Function<T,U> function,Collectors collector)
- Collectors.GroupingBy(,Collectors.counting())
- Collectors.GroupingBy(,mapping(function,Collector))
- Collectors.GroupingBy(Function<T,U>,filtering(Function ,Collector ))
- GroupingBy with Collect as a Map ( Map of String of Map )
-
toMap(Function key , Function value)
it doesn't support null keys or values if you provide a null key or null value, it will though null-pointer exceptions
-
filter
: Thefilter
method in Java 8 streams allows you to apply a condition or predicate to elements in a collection and retain only those elements that satisfy the condition. It is similar to anif
condition that filters elements based on a specific criterion. -
map
: Themap
method in Java 8 streams transforms each element in a collection by applying a given function to it. It is similar to usingget
to retrieve a value and then adding or putting the resultant transformed value into a collection. -
groupBy
collector usingmap.computeIfAbsent()
: By using thecomputeIfAbsent
method of theMap
interface in Java 8, you can implement a grouping operation where elements are grouped by a certain key. If the key is not present in the map, a value can be computed using the provided mapping function (Function<T, U>
). This allows you to create groups in the form of a map with keys and associated values. -
merge
method for implementinggroupBy
andcounting
: Themerge
method in Java 8 allows you to merge values into a map using a specified key and an operation. It is commonly used for grouping and counting elements in a collection. For example, you can merge values into a map where the key is a person's name, the initial value is 1, and the operation is to sum the values if a duplicate key is encountered. -
Modifications in Java 8
Map
collection: In Java 8, thetoMap
method in theCollectors
class is used to create a map from a stream. However, it has certain restrictions such as not allowing duplicate keys or values, as well as disallowing null keys and values. To handle duplicate keys, you can provide a key conflict resolution strategy using the third parameter ofCollectors.toMap
. This allows you to specify how to handle conflicts when two keys are the same.