Last active
November 18, 2016 19:34
-
-
Save iconte/b1df320c8870ad202810bae429fd3f96 to your computer and use it in GitHub Desktop.
Java 8 - Features
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
package br.com.teste.java8; | |
import java.util.Arrays; | |
import java.util.List; | |
/** | |
* Lambda encapsulando implementacoes de Comparator e Consumer | |
* | |
* @author icc | |
* | |
*/ | |
public class TesteLambda { | |
public static void main(String[] args) { | |
List<String> lista = Arrays.asList("Tiririca", | |
"Descobrimento do brasil", "Foo"); | |
// ordenação lexicografica | |
lista.sort((s1, s2) -> s1.compareTo(s2)); | |
// ordenação por tamanho | |
// lista.sort((s1, s2) -> Integer.compare(s1.length(), s2.length())); | |
// ordenação por tamanho desc | |
lista.sort((s1, s2) -> { | |
if (s1.length() < s2.length()) | |
return 1; | |
if (s1.length() > s2.length()) | |
return -1; | |
return 0; | |
}); | |
lista.forEach(s -> System.out.println(s)); | |
} | |
} |
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
package br.com.teste.java8; | |
import java.util.Arrays; | |
import java.util.Comparator; | |
import java.util.List; | |
public class TesteMethodReference { | |
public static void main(String[] args) { | |
List<String> lista = Arrays.asList("Tiririca", | |
"Descobrimento do brasil", "Foo"); | |
// ordenação lexicografica | |
// lista.sort((s1, s2) -> s1.compareTo(s2)); | |
//ordenação lexicografica - metodos auxiliares Comparator | |
// lista.sort(Comparator.reverseOrder()); | |
// ordenação por tamanho | |
// lista.sort((s1, s2) -> Integer.compare(s1.length(), s2.length())); | |
// ordenação por tamanho - method reference e metodos auxiliares Comparator | |
lista.sort(Comparator.comparing(String::length)); | |
// ordenação por tamanho desc | |
// lista.sort((s1, s2) -> { | |
// if (s1.length() < s2.length()) | |
// return 1; | |
// if (s1.length() > s2.length()) | |
// return -1; | |
// return 0; | |
// }); | |
// lista.forEach(s -> System.out.println(s)); | |
lista.forEach(System.out::println); | |
} | |
} |
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
package br.com.teste.java8; | |
import java.util.ArrayList; | |
import java.util.Comparator; | |
import java.util.List; | |
import java.util.Random; | |
public class TesteStream { | |
public static void main(String[] args) { | |
List<Aluno>alunos = new ArrayList<>(); | |
for (int i = 0; i < 100; i++) { | |
alunos.add(new Aluno("a"+i, new Random().nextInt(200))); | |
} | |
alunos.stream() | |
.filter(aluno -> aluno.getPontos() <50 && aluno.getPontos() >30) | |
.sorted(Comparator.comparing(Aluno::getPontos)) | |
.map(aluno->aluno.getPontos()) | |
.limit(100) | |
.forEach(pontuacao -> System.out.println(pontuacao)); | |
} | |
} | |
class Aluno{ | |
private String nome; | |
private int pontos; | |
public Aluno() { | |
} | |
public Aluno(String nome, int pontos) { | |
this.nome = nome; | |
this.pontos = pontos; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public int getPontos() { | |
return pontos; | |
} | |
public void setPontos(int pontos) { | |
this.pontos = pontos; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment