Skip to content

Commit

Permalink
feat(collections): 馃幐 calculations, translated
Browse files Browse the repository at this point in the history
Refers: #10
  • Loading branch information
rcmoutinho committed Sep 13, 2019
1 parent 2bc320e commit da1b2c7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
37 changes: 18 additions & 19 deletions book/08-java-collections/sections/04-calculations.asc
Original file line number Diff line number Diff line change
@@ -1,93 +1,92 @@
:java-package: src/org/j6toj8/collections
:section-java-package: ../../../{java-package}

=== Fazendo c谩lculos e coletando resultados de Streams
=== Perform Calculations and Collecting Streams Results

.Objetivo
.Objective
--------------------------------------------------
Perform calculations on Java Streams by using count, max, min, average, and sum methods and save results to a collection by using the collect method and Collector class, including the averagingDouble, groupingBy, joining, partitioningBy methods
-
Realizar c谩lculos em Streams usando os m茅todos count, max, min, average, e sum e salvar resultados em uma cole莽茫o usando o m茅todo collect e a classe Collector, incluindo os m茅todos averagingDouble, groupingBy, joining, partitioningBy
--------------------------------------------------

. 脡 poss铆vel pegar o maior ou menor valor, ou a quantidade de elementos da cole莽茫o.
. You can get the largest or smallest value, or the number of elements in the collection.
+
[source,java,indent=0]
.{java-package}/calculations/Collections_MaxMinCount.java
----
include::{section-java-package}/calculations/Collections_MaxMinCount.java[tag=code]
----
+
.Sa铆da no console
.console output
[source,console]
----
Max: 9
Min: 1
Count: 9
----

. 脡 poss铆vel pegar a m茅dia dos valores da cole莽茫o.
. You can take the average of the collection values.
+
[source,java,indent=0]
.{java-package}/calculations/Collections_AveragingDouble.java
----
include::{section-java-package}/calculations/Collections_AveragingDouble.java[tag=code]
----
+
.Sa铆da no console
.console output
[source,console]
----
M茅dia: 5.0
----

. 脡 poss铆vel agrupar os valores da cole莽茫o por uma regra espec铆fica.
. You can group collection values by a specific rule.
+
[source,java,indent=0]
.{java-package}/calculations/Collections_GroupingBy.java
----
include::{section-java-package}/calculations/Collections_GroupingBy.java[tag=code]
----
+
.Sa铆da no console
.console output
[source,console]
----
Mapa de resto da divis茫o por 3: {0=[3, 6, 9], 1=[1, 4, 7], 2=[2, 5, 8]}
Map of rest of division by 3: {0=[3, 6, 9], 1=[1, 4, 7], 2=[2, 5, 8]}
----

. 脡 poss铆vel concatenar os valores da cole莽茫o.
. You can concatenate the collection values.
+
[source,java,indent=0]
.{java-package}/calculations/Collections_Joining.java
----
include::{section-java-package}/calculations/Collections_Joining.java[tag=code]
----
+
.Sa铆da no console
.console output
[source,console]
----
Jun莽茫o dos valores como String: 123456789
Join values as String: 123456789
----

. 脡 poss铆vel separar os valores da cole莽茫o em um mapa com chaves `true` e `false`, de acordo com uma fun莽茫o lambda.
. You can separate collection values in a map with `true` and `false` keys, according to a lambda function.
+
[source,java,indent=0]
.{java-package}/calculations/Collections_PartitioningBy.java
----
include::{section-java-package}/calculations/Collections_PartitioningBy.java[tag=code]
----
+
.Sa铆da no console
.console output
[source,console]
----
Mapa de pares e 铆mpares: {false=[1, 3, 5, 7, 9], true=[2, 4, 6, 8]}
Even and odd map: {false=[1, 3, 5, 7, 9], true=[2, 4, 6, 8]}
----

.References
****
* Using Streams
+
Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 185). Wiley. Edi莽茫o do Kindle.
Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 185). Wiley. Kindle Edition.
* https://www.baeldung.com/java-8-streams[The Java 8 Stream API Tutorial.]
****
****
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public static void main(String[] args) {
// tag::code[]
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9);

Map<Integer, List<Integer>> mapaDivisaoPor3 = list.stream()
Map<Integer, List<Integer>> mapDivisionBy3 = list.stream()
.collect(Collectors.groupingBy(n -> n % 3));

System.out.println("Mapa de resto da divis茫o por 3: " + mapaDivisaoPor3);
System.out.println("Map of rest of division by 3: " + mapDivisionBy3);
// end::code[]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public static void main(String[] args) {
// tag::code[]
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9);

String juncao = list.stream()
String join = list.stream()
.map(n -> n.toString())
.collect(Collectors.joining());

System.out.println("Jun莽茫o dos valores como String: " + juncao);
System.out.println("Join values as String: " + join);
// end::code[]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9);

OptionalInt max = list.stream()
.mapToInt(Integer::intValue) // transforma para int
.mapToInt(Integer::intValue) // transform to int
.max();
System.out.println("Max: " + max.getAsInt());

OptionalInt min = list.stream()
.mapToInt(Integer::intValue) // transforma para int
.mapToInt(Integer::intValue) // transform to int
.min();
System.out.println("Min: " + min.getAsInt());

long count = list.stream()
.mapToInt(Integer::intValue) // transforma para int
.mapToInt(Integer::intValue) // transform to int
.count();
System.out.println("Count: " + count);
// end::code[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public static void main(String[] args) {
// tag::code[]
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9);

Map<Boolean, List<Integer>> mapaParImpar = list.stream()
Map<Boolean, List<Integer>> mapEvenOdd = list.stream()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));

System.out.println("Mapa de pares e 铆mpares: " + mapaParImpar);
System.out.println("Even and odd map: " + mapEvenOdd);
// end::code[]
}
}

0 comments on commit da1b2c7

Please sign in to comment.