Created
March 19, 2014 19:11
-
-
Save alexandreaquiles/9649033 to your computer and use it in GitHub Desktop.
Código do post "Menos Cerimônia e Mais Essência com Java 8": http://alexandreaquiles.com.br/2014/03/20/menos-cerimonia-e-mais-essencia-com-java-8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ComparacaoDeDesenhos { | |
boolean valePara(Desenho desenho); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Desenho { | |
private final String nome; | |
private final int decadaDeCriacao; | |
public Desenho(String nome, int decadaDeCriacao) { | |
this.nome = nome; | |
this.decadaDeCriacao = decadaDeCriacao; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public int getDecadaDeCriacao() { | |
return decadaDeCriacao; | |
} | |
public String toString() { | |
return nome + " (" + decadaDeCriacao + ")"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
class FiltroDeDesenhos { | |
public List<Desenho> filtra(List<Desenho> desenhos, ComparacaoDeDesenhos comparacao){ | |
List<Desenho> filtrados = new ArrayList<>(); | |
for (Desenho desenho : desenhos) { | |
if(comparacao.valePara(desenho)){ | |
filtrados.add(desenho); | |
} | |
} | |
return filtrados; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.List; | |
class ProgramaComLambdas { | |
public static void main(String[] args) { | |
Desenho popeye = new Desenho("Popeye", 1920); | |
Desenho picaPau = new Desenho("Pica-pau", 1940); | |
Desenho flintstones = new Desenho("Flintstones", 1960); | |
Desenho scoobyDoo = new Desenho("Scooby-Doo", 1970); | |
Desenho simpsons = new Desenho("Simpsons", 1990); | |
List<Desenho> desenhos = Arrays.asList(popeye, picaPau, flintstones, | |
scoobyDoo, simpsons); | |
FiltroDeDesenhos filtro = new FiltroDeDesenhos(); | |
List<Desenho> antesDe1960 = filtro.filtra(desenhos, | |
(desenho) -> desenho.getDecadaDeCriacao() < 1960 | |
); | |
for (Desenho desenho : antesDe1960) { | |
System.out.println(desenho); | |
} | |
List<Desenho> comecamComS = filtro.filtra(desenhos, | |
(desenho) -> desenho.getNome().startsWith("S") | |
); | |
for (Desenho desenho : comecamComS) { | |
System.out.println(desenho); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.List; | |
class ProgramaComStreams { | |
public static void main(String[] args) { | |
Desenho popeye = new Desenho("Popeye", 1920); | |
Desenho picaPau = new Desenho("Pica-pau", 1940); | |
Desenho flintstones = new Desenho("Flintstones", 1960); | |
Desenho scoobyDoo = new Desenho("Scooby-Doo", 1970); | |
Desenho simpsons = new Desenho("Simpsons", 1990); | |
List<Desenho> desenhos = Arrays.asList(popeye, picaPau, | |
flintstones, scoobyDoo, simpsons); | |
desenhos.stream() | |
.filter( desenho -> desenho.getDecadaDeCriacao() < 1960 ) | |
.forEach(System.out::println); | |
System.out.println("---------------"); | |
desenhos.stream() | |
.filter( desenho -> desenho.getNome().startsWith("S") ) | |
.forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment