Date: Tue, 20 Feb 2024 20:05:16 -0300
Subject: [PATCH 05/11] Fazendo tutorial JUnit5
---
Tests/JUnit5/AGuideToJUnit5/notes.md | 29 +++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git a/Tests/JUnit5/AGuideToJUnit5/notes.md b/Tests/JUnit5/AGuideToJUnit5/notes.md
index 653eb12..feefb44 100644
--- a/Tests/JUnit5/AGuideToJUnit5/notes.md
+++ b/Tests/JUnit5/AGuideToJUnit5/notes.md
@@ -115,7 +115,34 @@ static void done() {
5. Assertions and Assumptions
-JUnit5 tries to taku full advantage of the new features from Java 8, especially lambda expressions.
+JUnit5 tries to take full advantage of the new features from Java 8, especially lambda expressions.
+
+~~~Java
+@Test
+void lambdaExpressions() {
+ List numbers = Arrays.asList(1, 2, 3);
+ assertTrue(numbers.stream()
+ .mapToInt(Integer::intValue)
+ .sum() > 5, () -> "Sum should be greater than 5");
+}
+~~~
+
+Although the example above is trivial, one advantage of using the lambda expression for the assertion message is that itās lazily evaluated, which can save time and resources if the message construction is expensive.
+Itās also now possible to group assertions with assertAll(), which will report any failed assertions within the group with a MultipleFailuresError:
+
+~~~Java
+@Test
+ void groupAssertions() {
+ int[] numbers = {0, 1, 2, 3, 4};
+ assertAll("numbers",
+ () -> assertEquals(numbers[0], 1),
+ () -> assertEquals(numbers[3], 3),
+ () -> assertEquals(numbers[4], 1)
+ );
+ }
+~~~
+
+This means itās now safer to make more complex assertions, as weāll be able to pinpoint the exact location of any failure.
From f0a213b3ced7d2a3e12ced8a622e642712d58bb8 Mon Sep 17 00:00:00 2001
From: LoriaLawrenceZ
Date: Tue, 20 Feb 2024 20:44:39 -0300
Subject: [PATCH 06/11] =?UTF-8?q?Fazendo=20Introdu=C3=A7=C3=A3o=20=C3=A0?=
=?UTF-8?q?=20Streams?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../StreamBasics/IntroductionToStreams.md | 28 +++++++++++++++++++
.../Streams/StreamBasics/StreamAPITutorial.md | 9 ++++++
2 files changed, 37 insertions(+)
create mode 100644 BackToBasics/Streams/StreamBasics/IntroductionToStreams.md
create mode 100644 BackToBasics/Streams/StreamBasics/StreamAPITutorial.md
diff --git a/BackToBasics/Streams/StreamBasics/IntroductionToStreams.md b/BackToBasics/Streams/StreamBasics/IntroductionToStreams.md
new file mode 100644
index 0000000..43ee87d
--- /dev/null
+++ b/BackToBasics/Streams/StreamBasics/IntroductionToStreams.md
@@ -0,0 +1,28 @@
+# **1. Overview**
+
+In this article, weāll have a quick look at one of the major pieces of new functionality that Java 8 had added ā Streams.
+
+Weāll explain what streams are about and showcase the creation and basic stream operations with simple examples.
+
+
+# **2. Stream API**
+
+One of the major new features in Java 8 is the introduction of the stream functionality ā [java.util.stream](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/stream/package-summary.html) ā which contains classes for processing sequences of elements.
+
+The central API class is the [Stream](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/stream/Stream.html). The following section will demonstrate how streams can be created using the existing data-provider sources.
+
+## **2.1. Stream Creation**
+
+Streams can be created from different element sources e.g. collections or arrays with the help of stream() and of() methods:
+
+~~~Java
+String[] arr = new String[]{"a", "b", "c"};
+Stream stream = Arrays.stream(arr);
+stream = Stream.of("a", "b", "c");
+~~~
+
+A stream() default method is added to the Collection interface and allows creating a Stream using any collection as an element source:
+
+~~~Java
+Stream stream = list.stream();
+~~~
\ No newline at end of file
diff --git a/BackToBasics/Streams/StreamBasics/StreamAPITutorial.md b/BackToBasics/Streams/StreamBasics/StreamAPITutorial.md
new file mode 100644
index 0000000..1110cc2
--- /dev/null
+++ b/BackToBasics/Streams/StreamBasics/StreamAPITutorial.md
@@ -0,0 +1,9 @@
+# **1. Overview**
+
+In this comprehensive tutorial, weāll go through the practical uses of Java 8 Streams from creation to parallel execution.
+
+To understand this material, readers need to have a basic knowledge of Java 8 (lambda expressions, Optional, method references) and of the Stream API. In order to be more familiar with these topics, please take a look at our previous articles: [New Features in Java 8](https://www.baeldung.com/java-8-new-features) and [Introduction to Java 8 Streams](https://www.baeldung.com/java-8-streams-introduction).
+
+# **2. Stream Creation**
+
+There are many ways to create a stream instance of different sources. Once created, the instance will not modify its source, therefore allowing the creation of multiple instances from a single source.
From 660ecbc3726fbb2023e3ecd9b0c5e6d1b34befe3 Mon Sep 17 00:00:00 2001
From: LoriaLawrenceZ
Date: Tue, 20 Feb 2024 21:05:20 -0300
Subject: [PATCH 07/11] Introduction Streams
---
.../StreamBasics/IntroductionToStreams.md | 162 +++++++++++++++++-
1 file changed, 156 insertions(+), 6 deletions(-)
diff --git a/BackToBasics/Streams/StreamBasics/IntroductionToStreams.md b/BackToBasics/Streams/StreamBasics/IntroductionToStreams.md
index 43ee87d..a0bc2cf 100644
--- a/BackToBasics/Streams/StreamBasics/IntroductionToStreams.md
+++ b/BackToBasics/Streams/StreamBasics/IntroductionToStreams.md
@@ -1,19 +1,19 @@
# **1. Overview**
-In this article, weāll have a quick look at one of the major pieces of new functionality that Java 8 had added ā Streams.
+In this article, weāll have a quick look at one of the major pieces of new functionality that Java 8 had added *ā Stream*s.
-Weāll explain what streams are about and showcase the creation and basic stream operations with simple examples.
+Weāll explain what streams are about and showcase the creation and *basic stream* operations with simple examples.
# **2. Stream API**
One of the major new features in Java 8 is the introduction of the stream functionality ā [java.util.stream](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/stream/package-summary.html) ā which contains classes for processing sequences of elements.
-The central API class is the [Stream](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/stream/Stream.html). The following section will demonstrate how streams can be created using the existing data-provider sources.
+The central API class is the [*Stream*](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/*stream*/*Stream*.html). The following section will demonstrate how streams can be created using the existing data-provider sources.
## **2.1. Stream Creation**
-Streams can be created from different element sources e.g. collections or arrays with the help of stream() and of() methods:
+Streams can be created from different element sources e.g. *collections* or arrays with the help of *stream()* and *of()* methods:
~~~Java
String[] arr = new String[]{"a", "b", "c"};
@@ -21,8 +21,158 @@ Stream stream = Arrays.stream(arr);
stream = Stream.of("a", "b", "c");
~~~
-A stream() default method is added to the Collection interface and allows creating a Stream using any collection as an element source:
+A *stream()* default method is added to the *Collection* interface and allows creating a *Stream* using any *collection* as an element source:
~~~Java
Stream stream = list.stream();
-~~~
\ No newline at end of file
+~~~
+
+## **2.2. Multi-threading With Streams**
+
+Stream API also simplifies multithreading by providing the *parallelStream()* method that runs operations over the streamās elements in parallel mode.
+
+The code below allows us to run method *doWork()* in parallel for every element of the *stream*:
+
+~~~Java
+list.parallelStream().forEach(element -> doWork(element));
+~~~
+
+In the following section, we will introduce some of the *basic Stream* API operations.
+
+# **3. Stream Operations**
+
+There are many useful operations that can be performed on a *stream*.
+
+They are divided into intermediate operations (return *Stream*) and terminal operations (return a result of definite type). Intermediate operations allow chaining.
+
+Itās also worth noting that operations on streams donāt change the source.
+
+Hereās a quick example:
+
+~~~Java
+long count = list.stream().distinct().count();
+~~~
+
+So, the *distinct()* method represents an intermediate operation, which creates a new stream of unique elements of the previous *stream*. And the *count()* method is a terminal operation, which returns streamās size.
+
+## **3.1. Iterating**
+
+Stream API helps to substitute for, for-each, and while loops. It allows concentrating on operationās logic, but not on the iteration over the sequence of elements. For example:
+
+~~~Java
+for (String string : list) {
+ if (string.contains("a")) {
+ return true;
+ }
+}
+~~~
+
+This code can be changed just with one line of Java 8 code:
+
+~~~Java
+boolean isExist = list.stream().anyMatch(element -> element.contains("a"));
+~~~
+
+## **3.2. Filtering**
+
+The *filter()* method allows us to pick a stream of elements that satisfy a predicate.
+
+For example, consider the following list:
+
+~~~Java
+ArrayList list = new ArrayList<>();
+list.add("One");
+list.add("OneAndOnly");
+list.add("Derek");
+list.add("Change");
+list.add("factory");
+list.add("justBefore");
+list.add("Italy");
+list.add("Italy");
+list.add("Thursday");
+list.add("");
+list.add("");
+~~~
+
+The following code creates a *Stream* of the List, finds all elements of this stream which contain char ādā, and creates a new stream containing only the filtered elements:
+
+~~~Java
+Stream stream = list.stream().filter(element -> element.contains("d"));
+~~~
+
+## **3.3. Mapping**
+
+To convert elements of a Stream by applying a special function to them and to collect these new elements into a *Stream*, we can use the *map()* method:
+
+~~~Java
+List uris = new ArrayList<>();
+uris.add("C:\\My.txt");
+Stream stream = uris.stream().map(uri -> Paths.get(uri));
+~~~
+
+So, the code above converts *Stream* to the *Stream* by applying a specific lambda expression to every element of the initial *Stream*.
+
+If you have a *stream* where every element contains its own sequence of elements and you want to create a *stream* of these inner elements, you should use the *flatMap()* method:
+
+~~~Java
+List details = new ArrayList<>();
+details.add(new Detail());
+Stream stream
+ = details.stream().flatMap(detail -> detail.getParts().stream());
+~~~
+
+In this example, we have a list of elements of type Detail. The Detail class contains a field PARTS, which is a *List*. With the help of the flatMap() method, every element from field PARTS will be extracted and added to the new resulting *stream*. After that, the initial *Stream* will be lost.
+
+## **3.4. Matching**
+
+Stream API gives a handy set of instruments to validate elements of a sequence according to some predicate. To do this, one of the following methods can be used: *anyMatch()*, *allMatch()*, *noneMatch()*. Their names are self-explanatory. Those are terminal operations that return a boolean:
+
+~~~Java
+boolean isValid = list.stream().anyMatch(element -> element.contains("h")); // true
+boolean isValidOne = list.stream().allMatch(element -> element.contains("h")); // false
+boolean isValidTwo = list.stream().noneMatch(element -> element.contains("h")); // false
+~~~
+
+For empty streams, the *allMatch()* method with any given predicate will return true:
+
+~~~Java
+Stream.empty().allMatch(Objects::nonNull); // true
+~~~
+
+This is a sensible default, as we canāt find any element that doesnāt satisfy the predicate.
+
+Similarly, the *anyMatch()* method always returns false for empty streams:
+
+~~~Java
+Stream.empty().anyMatch(Objects::nonNull); // false
+~~~
+
+Again, this is reasonable, as we canāt find an element satisfying this condition.
+
+## **3.5. Reduction**
+*
+Stream* API allows reducing a sequence of elements to some value according to a specified function with the help of the *reduce()* method of the type *Stream*. This method takes two parameters: first ā start value, second ā an accumulator function.
+
+Imagine that you have a *List* and you want to have a sum of all these elements and some initial Integer (in this example 23). So, you can run the following code and result will be 26 (23 + 1 + 1 + 1).
+
+~~~Java:
+List integers = Arrays.asList(1, 1, 1);
+Integer reduced = integers.stream().reduce(23, (a, b) -> a + b);
+~~~
+
+## **3.6. Collecting**
+
+The reduction can also be provided by the *collect()* method of type *Stream*. This operation is very handy in case of converting a stream to a *Collection* or a Map and representing a stream in the form of a single string. There is a utility class Collectors which provide a solution for almost all typical collecting operations. For some, not trivial tasks, a custom Collector can be created.
+
+~~~Java
+List resultList
+ = list.stream().map(element -> element.toUpperCase()).collect(Collectors.toList());
+~~~
+
+This code uses the terminal collect() operation to reduce a *Stream* to the *List*.
+
+# **4. Conclusions**
+
+In this article, we briefly touched upon Java streams ā definitely one of the most interesting Java 8 features.
+
+There are many more advanced examples of using Streams; the goal of this write-up was only to provide a quick and practical introduction to what you can start doing with the functionality and as a starting point for exploring and further learning.
\ No newline at end of file
From 460efc71aa084e37605cc0535566cdd1cb25584f Mon Sep 17 00:00:00 2001
From: LoriaLawrenceZ
Date: Tue, 20 Feb 2024 21:08:48 -0300
Subject: [PATCH 08/11] Introduction Streams
---
README.md | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/README.md b/README.md
index 6e62d5f..e079d56 100644
--- a/README.md
+++ b/README.md
@@ -10,6 +10,17 @@
Tests
+ -
+
+ Back To Basics (Baeldung summary page)
+
+
+
+
-
JUnit5 (Baeldung summary page)
From 2db5145e7a36ce954159c014000f4b127ead2d34 Mon Sep 17 00:00:00 2001
From: "Lorenzo O. Zimbres" <97912499+LoriaLawrenceZ@users.noreply.github.com>
Date: Tue, 20 Feb 2024 21:09:39 -0300
Subject: [PATCH 09/11] Update README.md
---
README.md | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/README.md b/README.md
index e079d56..40b4b3b 100644
--- a/README.md
+++ b/README.md
@@ -13,11 +13,18 @@
-
Back To Basics (Baeldung summary page)
+
From d1adf8d9bc1de1e5dd4d6ebc3cb3c7e2d985c775 Mon Sep 17 00:00:00 2001
From: "Lorenzo O. Zimbres" <97912499+LoriaLawrenceZ@users.noreply.github.com>
Date: Tue, 20 Feb 2024 21:15:25 -0300
Subject: [PATCH 10/11] Update README.md
---
README.md | 17 ++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)
diff --git a/README.md b/README.md
index 40b4b3b..f56c887 100644
--- a/README.md
+++ b/README.md
@@ -16,13 +16,16 @@
From 9ce39397494cf0d6dc1fbfd54f28eebf1eeb9346 Mon Sep 17 00:00:00 2001
From: "Lorenzo O. Zimbres" <97912499+LoriaLawrenceZ@users.noreply.github.com>
Date: Tue, 20 Feb 2024 21:16:00 -0300
Subject: [PATCH 11/11] Update README.md
---
README.md | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index f56c887..576baf9 100644
--- a/README.md
+++ b/README.md
@@ -19,10 +19,10 @@
5. Java Streams (Baeldung summary page)
@@ -36,7 +36,7 @@
JUnit5 (Baeldung summary page)
@@ -47,7 +47,7 @@
Mockito (Baeldung summary page)