Skip to content

Commit

Permalink
feat(collections): 🎸 collection improvements, translated
Browse files Browse the repository at this point in the history
Refers: #10
  • Loading branch information
rcmoutinho committed Sep 13, 2019
1 parent da1b2c7 commit 731a4ad
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
45 changes: 22 additions & 23 deletions book/08-java-collections/sections/05-collection-improvements.asc
Original file line number Diff line number Diff line change
@@ -1,83 +1,82 @@
:java-package: src/org/j6toj8/collections
:section-java-package: ../../../{java-package}

=== Melhorias de Java 8 em Coleções
=== Java 8 collection improvements

.Objetivo
.Objective
--------------------------------------------------
Develop code that uses Java SE 8 collection improvements, including the Collection.removeIf(), List.replaceAll(), Map.computeIfAbsent(), and Map.computeIfPresent() methods
-
Desenvolver código que use as melhorias em coleções do Java SE 8, incluindo os métodos Collection.removeIf(), List.replaceAll(), Map.computeIfAbsent(), e Map.computeIfPresent()
--------------------------------------------------

O Java 8 trouxe vários métodos em Collections que utilizam como argumento uma função lambda, facilitando algumas operações. Serão apresentados exemplos dos 4 métodos relacionados a esse objetivo.
Java 8 has introduced several methods in Collections that use a lambda function as argument, making some operations easier. Examples of the 4 methods related to this objective will be presented.

. É possível remover um item de uma coleção condicionalmente com uma função lambda.
. You can conditionally remove an item from a collection with a lambda function.
+
[source,java,indent=0]
.{java-package}/improvements/Collections_RemoveIf.java
----
include::{section-java-package}/improvements/Collections_RemoveIf.java[tag=code]
----
+
.Saída no console
.console output
[source,console]
----
Lista antes do removeIf: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Lista depois do removeIf: [1, 3, 5, 7, 9]
List before removeIf: [1, 2, 3, 4, 5, 6, 7, 8, 9]
List after removeIf: [1, 3, 5, 7, 9]
----

. É possível modificar todos os elementos da coleção de acordo com uma operação lambda.
. You can modify all elements of the collection according to a lambda operation.
+
[source,java,indent=0]
.{java-package}/improvements/Collections_ReplaceAll.java
----
include::{section-java-package}/improvements/Collections_ReplaceAll.java[tag=code]
----
+
.Saída no console
.console output
[source,console]
----
Lista antes do replaceAll: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Lista depois do replaceAll: [2, 4, 6, 8, 10, 12, 14, 16, 18]
List before replaceAll: [1, 2, 3, 4, 5, 6, 7, 8, 9]
List after replaceAll: [2, 4, 6, 8, 10, 12, 14, 16, 18]
----

. É possível colocar valores em um `Map` a partir de uma função lambda, apenas se a chave *não* estiver presente no `Map`.
. You can put values in a `Map` from a lambda function only if the *key* is not present in `Map`.
+
[source,java,indent=0]
.{java-package}/improvements/Collections_ComputeIfAbsent.java
----
include::{section-java-package}/improvements/Collections_ComputeIfAbsent.java[tag=code]
----
+
.Saída no console
.console output
[source,console]
----
Map antes do computeIfAbsent: {A=65, B=66}
Map depois do computeIfAbsent: {A=65, B=66, C=67}
Map before computeIfAbsent: {A=65, B=66}
Map after computeIfAbsent: {A=65, B=66, C=67}
----

. É possível alterar valores em um `Map` a partir de uma função lambda, apenas se a chave estiver presente no `Map`.
. You can change values in a `Map` from a lambda function only if the key is present in `Map`.
+
[source,java,indent=0]
.{java-package}/improvements/Collections_ComputeIfPresent.java
----
include::{section-java-package}/improvements/Collections_ComputeIfPresent.java[tag=code]
----
+
.Saída no console
.console output
[source,console]
----
Map antes do computeIfPresent: {A=65, B=66}
Map depois do computeIfPresent: {A=4225, B=4356}
Map before computeIfPresent: {A=65, B=66}
Map after computeIfPresent: {A=4225, B=4356}
----

.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 @@ -10,10 +10,10 @@ public static void main(String[] args) {
map.put("A", "A".hashCode());
map.put("B", "B".hashCode());

System.out.println("Map antes do computeIfAbsent: " + map);
System.out.println("Map before computeIfAbsent: " + map);
map.computeIfAbsent("A", k -> k.hashCode());
map.computeIfAbsent("C", k -> k.hashCode());
System.out.println("Map depois do computeIfAbsent: " + map);
System.out.println("Map after computeIfAbsent: " + map);
// end::code[]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public static void main(String[] args) {
map.put("A", "A".hashCode());
map.put("B", "B".hashCode());

System.out.println("Map antes do computeIfPresent: " + map);
System.out.println("Map before computeIfPresent: " + map);
// k = chave; v = valor
map.computeIfPresent("A", (k, v) -> k.hashCode() * v);
map.computeIfPresent("B", (k, v) -> k.hashCode() * v);
map.computeIfPresent("C", (k, v) -> k.hashCode() * v);
System.out.println("Map depois do computeIfPresent: " + map);
System.out.println("Map after computeIfPresent: " + map);
// end::code[]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public static void main(String[] args) {
// tag::code[]
List<Integer> list = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9));

System.out.println("Lista antes do removeIf: " + list);
list.removeIf(n -> n % 2 == 0); // remove números pares
System.out.println("Lista depois do removeIf: " + list);
System.out.println("List before removeIf: " + list);
list.removeIf(n -> n % 2 == 0); // remove even numbers
System.out.println("List after removeIf: " + list);
// end::code[]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public static void main(String[] args) {
// tag::code[]
List<Integer> list = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9));

System.out.println("Lista antes do replaceAll: " + list);
list.replaceAll(n -> n * 2); // multiplica todos os elementos por 2
System.out.println("Lista depois do replaceAll: " + list);
System.out.println("List before replaceAll: " + list);
list.replaceAll(n -> n * 2); // multiply all elements by 2
System.out.println("List after replaceAll: " + list);
// end::code[]
}
}

0 comments on commit 731a4ad

Please sign in to comment.