![Page 1: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/1.jpg)
Globalcode – Open4education
Java 8 Afinal onde usamos no dia a dia?
Frederico Maia Arantes Desenvolvedor de Sofware / Instrutor
![Page 2: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/2.jpg)
Globalcode – Open4education
Sobre mim
Frederico Maia Arantes Goiano, atualmente no Rio de Janeiro +7 anos de experiência em desenvolvimento Desenvolvedor de software no LIP da TecSinapse Instrutor de cursos Java na Caelum Estudante - MBA em Engenharia de Software na UFRJ
![Page 3: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/3.jpg)
Globalcode – Open4education
Sobre a palestra
Default Methods Lambdas Expressions Functional Interfaces Streams Collectors
![Page 4: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/4.jpg)
Globalcode – Open4education
Classe ‘Ingresso’ public class Ingresso { private String nomeEvento; private double valor; public Ingresso(String nomeEvento, double valor) { this.nomeEvento = nomeEvento; this.valor = valor; } //getters e setters //toString() }
![Page 5: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/5.jpg)
Globalcode – Open4education
Loop tradicional public static void main(String[] args) { List<Ingresso> ingressos = Arrays.asList(
new Ingresso("Festa 1", 100.0), new Ingresso("Festa 2", 150.0), new Ingresso("Festa 3", 200.0));
for(Ingresso ingresso : ingressos) { System.out.println(ingresso); } }
![Page 6: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/6.jpg)
Globalcode – Open4education
Novo loop com forEach public static void main(String[] args) { List<Ingresso> ingressos = Arrays.asList(
new Ingresso("Festa 1", 100.0), new Ingresso("Festa 2", 150.0), new Ingresso("Festa 3", 200.0));
ingressos.forEach(consumer); //recebe um Consumer //Consumer é uma nova interface do Java 8 com um único método }
![Page 7: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/7.jpg)
Globalcode – Open4education
Novo loop com forEach public static void main(String[] args) { List<Ingresso> ingressos = Arrays.asList(
new Ingresso("Festa 1", 100.0), new Ingresso("Festa 2", 150.0), new Ingresso("Festa 3", 200.0));
Consumer<Ingresso> consumer = new Consumer<Ingresso>() { @Override public void accept(Ingresso ingresso) { System.out.println(ingresso); } }; ingressos.forEach(consumer); //recebe um Consumer }
![Page 8: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/8.jpg)
Globalcode – Open4education
Novo loop com forEach public static void main(String[] args) { List<Ingresso> ingressos = Arrays.asList(
new Ingresso("Festa 1", 100.0), new Ingresso("Festa 2", 150.0), new Ingresso("Festa 3", 200.0));
ingressos.forEach(new Consumer<Ingresso>() { @Override public void accept(Ingresso ingresso) { System.out.println(ingresso); } }); }
![Page 9: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/9.jpg)
Globalcode – Open4education
Remover ingressos caros public static void main(String[] args) { List<Ingresso> ingressos = new ArrayList<>(); ingressos.add(new Ingresso("Festa 1", 100.0)); ingressos.add(new Ingresso("Festa 2", 150.0)); ingressos.add(new Ingresso("Festa 3", 180.0)); ingressos.removeIf(new Predicate<Ingresso>() { @Override public boolean test(Ingresso ingresso) { return ingresso.getValor() > 100; } }); }
![Page 10: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/10.jpg)
Globalcode – Open4education
Default methods
Novos métodos, com implementação, adicionados às interfaces Java, como em Collections e Map. Veja por exemplo o forEach:
default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } }
Outros: removeIf, sort, compare, computeIfPresente. Você pode criar default methods em suas interfaces.
![Page 11: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/11.jpg)
Globalcode – Open4education
Legais os Default Methods!
Mas agora preciso escrever mais código que antes, pra fazer a mesma coisa.
![Page 12: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/12.jpg)
Globalcode – Open4education
Lambdas
O que são? De onde vem? De que se alimentam?
“Simplificando bastante, um lambda no Java é uma maneira mais simples de implementar uma interface que tem um
único método.”
![Page 13: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/13.jpg)
Globalcode – Open4education
Lambdas public static void main(String[] args) { List<Ingresso> ingressos = Arrays.asList(
new Ingresso("Festa 1", 100.0), new Ingresso("Festa 2", 150.0), new Ingresso("Festa 3", 200.0));
ingressos.forEach(new Consumer<Ingresso>() { @Override public void accept(Ingresso ingresso) { System.out.println(ingresso); } }); }
![Page 14: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/14.jpg)
Globalcode – Open4education
Lambdas public static void main(String[] args) { List<Ingresso> ingressos = Arrays.asList(
new Ingresso("Festa 1", 100.0), new Ingresso("Festa 2", 150.0), new Ingresso("Festa 3", 200.0));
ingressos.forEach(new Consumer<Ingresso>() { public void accept(Ingresso ingresso) { System.out.println(ingresso); } }); }
![Page 15: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/15.jpg)
Globalcode – Open4education
Lambdas public static void main(String[] args) { List<Ingresso> ingressos = Arrays.asList(
new Ingresso("Festa 1", 100.0), new Ingresso("Festa 2", 150.0), new Ingresso("Festa 3", 200.0));
ingressos.forEach( { public void accept(Ingresso ingresso) { System.out.println(ingresso); } }); }
![Page 16: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/16.jpg)
Globalcode – Open4education
Lambdas public static void main(String[] args) { List<Ingresso> ingressos = Arrays.asList(
new Ingresso("Festa 1", 100.0), new Ingresso("Festa 2", 150.0), new Ingresso("Festa 3", 200.0));
ingressos.forEach( (Ingresso ingresso) { System.out.println(ingresso); }); }
![Page 17: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/17.jpg)
Globalcode – Open4education
Lambdas public static void main(String[] args) { List<Ingresso> ingressos = Arrays.asList(
new Ingresso("Festa 1", 100.0), new Ingresso("Festa 2", 150.0), new Ingresso("Festa 3", 200.0));
ingressos.forEach( (Ingresso ingresso) -‐> { System.out.println(ingresso) } ); }
![Page 18: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/18.jpg)
Globalcode – Open4education
Lambdas public static void main(String[] args) { List<Ingresso> ingressos = Arrays.asList(
new Ingresso("Festa 1", 100.0), new Ingresso("Festa 2", 150.0), new Ingresso("Festa 3", 200.0));
ingressos.forEach( ingresso -‐> { System.out.println(ingresso) } ); }
![Page 19: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/19.jpg)
Globalcode – Open4education
Lambdas public static void main(String[] args) { List<Ingresso> ingressos = Arrays.asList(
new Ingresso("Festa 1", 100.0), new Ingresso("Festa 2", 150.0), new Ingresso("Festa 3", 200.0));
ingressos.forEach(ingresso -‐> System.out.println(ingresso)); }
![Page 20: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/20.jpg)
Globalcode – Open4education
Functional Interfaces
Interfaces com um único método abstrato. Este é um requisito obrigatório. Exemplo: Consumer. Assim no forEach o compilador sabe o método que deverá ser implementado no lambda. Funciona só em interfaces novas do Java 8? Não. Para as novas, e as suas implementações, temos a anotação opcional @FunctionalInterface.
![Page 21: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/21.jpg)
Globalcode – Open4education
Functional Interfaces
button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e){ System.out.println("clik!"); } }); //Com Lambda button.addActionListener(e -‐> System.out.println("click!"));
![Page 22: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/22.jpg)
Globalcode – Open4education
Streams
Trabalhar com coleções sempre envolve muito código. Em muitas linguagens temos por exemplo um ‘filter’ pra facilitar nossa vida. Forma mais funcional de lidar com coleções.
![Page 23: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/23.jpg)
Globalcode – Open4education
Filtrar da forma tradicional public static void main(String[] args) { List<Ingresso> ingressos = new ArrayList<>(); ingressos.add(new Ingresso("Festa 1", 100.0)); ingressos.add(new Ingresso("Festa 2", 150.0)); ingressos.add(new Ingresso("Festa 3", 180.0)); List<Ingresso> ingressosBaratos = new ArrayList<>(); for(Ingresso ingresso : ingressos) { if(ingresso.getValor() < 120) { ingressosBaratos.add(ingresso); } } }
![Page 24: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/24.jpg)
Globalcode – Open4education
Filtrar com Stream public static void main(String[] args) { List<Ingresso> ingressos = new ArrayList<>(); ingressos.add(new Ingresso("Festa 1", 100.0)); ingressos.add(new Ingresso("Festa 2", 150.0)); ingressos.add(new Ingresso("Festa 3", 180.0)); Stream<Ingresso> stream = ingressos.stream(); }
![Page 25: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/25.jpg)
Globalcode – Open4education
Filtrar com Stream public static void main(String[] args) { List<Ingresso> ingressos = new ArrayList<>(); ingressos.add(new Ingresso("Festa 1", 100.0)); ingressos.add(new Ingresso("Festa 2", 150.0)); ingressos.add(new Ingresso("Festa 3", 180.0)); Stream<Ingresso> stream = ingressos.stream(); stream.filter( i -‐> i.getValor() < 120 ); }
![Page 26: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/26.jpg)
Globalcode – Open4education
Filtrar com Stream public static void main(String[] args) { List<Ingresso> ingressos = new ArrayList<>(); ingressos.add(new Ingresso("Festa 1", 100.0)); ingressos.add(new Ingresso("Festa 2", 150.0)); ingressos.add(new Ingresso("Festa 3", 180.0)); ingressos.stream().filter( i -‐> i.getValor() < 120 ); }
![Page 27: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/27.jpg)
Globalcode – Open4education
Filtrar com Stream public static void main(String[] args) { List<Ingresso> ingressos = new ArrayList<>(); ingressos.add(new Ingresso("Festa 1", 100.0)); ingressos.add(new Ingresso("Festa 2", 150.0)); ingressos.add(new Ingresso("Festa 3", 180.0)); ingressos.stream().filter( i -‐> i.getValor() < 120 ); ingressos.forEach( i -‐> System.out.println(i) ); //imprime Festa 1, Festa 2 e Festa 3 }
![Page 28: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/28.jpg)
Globalcode – Open4education
Filtrar com Stream public static void main(String[] args) { List<Ingresso> ingressos = new ArrayList<>(); ingressos.add(new Ingresso("Festa 1", 100.0)); ingressos.add(new Ingresso("Festa 2", 150.0)); ingressos.add(new Ingresso("Festa 3", 180.0)); ingressos.stream() .filter( i -‐> i.getValor() < 120 ) .forEach( i -‐> System.out.println(i) ); //imprime Festa 1 }
![Page 29: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/29.jpg)
Globalcode – Open4education
Collectors, obtendo a lista public static void main(String[] args) { List<Ingresso> ingressos = new ArrayList<>(); ingressos.add(new Ingresso("Festa 1", 100.0)); ingressos.add(new Ingresso("Festa 2", 150.0)); ingressos.add(new Ingresso("Festa 3", 180.0)); ingressos.stream() .filter( i -‐> i.getValor() < 120 ) .collect(Collectors.toList()); }
![Page 30: Java 8 - Afinal onde usamos no dia a dia? TDC 2015 - Porto Alegre](https://reader033.vdocuments.pub/reader033/viewer/2022052915/58ec4f0a1a28abba7c8b463d/html5/thumbnails/30.jpg)
Globalcode – Open4education
Collectors, obtendo a lista public static void main(String[] args) { List<Ingresso> ingressos = new ArrayList<>(); ingressos.add(new Ingresso("Festa 1", 100.0)); ingressos.add(new Ingresso("Festa 2", 150.0)); ingressos.add(new Ingresso("Festa 3", 180.0)); List<Ingressos> ingressosBaratos = ingressos.stream() .filter( i -‐> i.getValor() < 120 ) .collect(Collectors.toList()); }