Skip to content

Commit

Permalink
feat(collections): 🎸 diamond, translated
Browse files Browse the repository at this point in the history
Refers: #10
  • Loading branch information
rcmoutinho committed Sep 13, 2019
1 parent 9b60c68 commit c7dd38c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
13 changes: 6 additions & 7 deletions book/08-java-collections/sections/01-diamond.asc
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
:java-package: src/org/j6toj8/collections
:section-java-package: ../../../{java-package}

=== Diamond Operator (Operador Diamante)
=== Diamond Operator

.Objetivo
.Objective
--------------------------------------------------
Develop code that uses diamond with generic declarations
-
Desenvolver código que usa o diamond (diamante) com declarações de generics
--------------------------------------------------

O Diamond Operator (ou Operador Diamante) foi criado no Java 7 para remover código desnecessário ao declarar classes que usam `Generics` (ou tipos genéricos). Abaixo um exemplo que é possível omitir o tipo de algumas classes utilizando o _Diamond Operator_.
Diamond Operator was created in Java 7 to remove unnecessary code when declaring classes that use `Generics` (or generic types). Below is an example that you can omit the type of some classes using _Diamond Operator_.

[source,java,indent=0]
.{java-package}/diamond/Collections_Diamond.java
----
include::{section-java-package}/diamond/Collections_Diamond.java[tag=code]
----

.References
****
* Using the Diamond Operator
+
Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 596). Wiley. Edição do Kindle.
Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 596). Wiley. Kindle Edition.
* https://www.baeldung.com/java-diamond-operator[Guide to the Diamond Operator in Java.]
****
****
24 changes: 12 additions & 12 deletions src/org/j6toj8/collections/diamond/Collections_Diamond.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ public class Collections_Diamond {

public static void main(String[] args) {
// tag::code[]
List<String> l1 = new ArrayList<String>(); // sem diamond
List<String> l2 = new ArrayList<>(); // com diamond
List<> l3 = new ArrayList<String>(); // NÃO COMPILA - diamond só pode ser utilizado do lado direito
List<String> l1 = new ArrayList<String>(); // without diamond
List<String> l2 = new ArrayList<>(); // with diamond
List<> l3 = new ArrayList<String>(); // NOT COMPILING - diamond can only be used on the right side

Map<String, String> m1 = new HashMap<String, String>(); // sem diamond
Map<String, String> m2 = new HashMap<>(); // com diamond
Map<> m3 = new HashMap<String, String>(); // NÃO COMPILA - diamond só do lado direito
Map<String, String> m1 = new HashMap<String, String>(); // without diamond
Map<String, String> m2 = new HashMap<>(); // with diamond
Map<> m3 = new HashMap<String, String>(); // NOT COMPILING - diamond can only be used on the right side

Map<List<String>, List<String>> m4 = new HashMap<List<String>, List<String>>(); // sem diamond
Map<List<String>, List<String>> m5 = new HashMap<>(); // com diamond
Map<List<String>, List<String>> m6 = new HashMap<<>,<>>(); // NÃO COMPILA - a única sintaxe válida é <>
Map<List<String>, List<String>> m7 = new HashMap<List<String>, <>>(); // NÃO COMPILA - a única sintaxe válida é <>
Map<List<String>, List<String>> m4 = new HashMap<List<String>, List<String>>(); // without diamond
Map<List<String>, List<String>> m5 = new HashMap<>(); // with diamond
Map<List<String>, List<String>> m6 = new HashMap<<>,<>>(); // NOT COMPILING - the only valid syntax is <>
Map<List<String>, List<String>> m7 = new HashMap<List<String>, <>>(); // NOT COMPILING - the only valid syntax is <>

Map<Map<List<String>, List<String>>, Map<List<String>, List<String>>> m8 = new HashMap<>(); // com diamond
Map<Map<List<String>, List<String>>, Map<List<String>, List<String>>> m8 = new HashMap<>(); // with diamond

Map<> m9 = new HashMap<>(); // NÃO COMPILA - é necessário informar o tipo do lado esquerdo
Map<> m9 = new HashMap<>(); // NOT COMPILING - it's necessary to inform the type on the left side
// end::code[]
}

Expand Down

0 comments on commit c7dd38c

Please sign in to comment.