Last active
November 23, 2015 22:59
-
-
Save windoctor/650f566bfcc3617f0913 to your computer and use it in GitHub Desktop.
Código utilizado en el artículo http://windoctor.github.io/api-stream-java8-parte1.html
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.*; | |
import java.util.stream.*; | |
public class StreamJava8 { | |
public StreamJava8(){ | |
List<Cancion> canciones = new ArrayList<>(Arrays.asList( | |
new Cancion("Te Equivocas", "Jon Carlo"), | |
new Cancion("Solo tu", "Orlando Elizalde"), | |
new Cancion("La Mano de Dios","Jon Carlo"), | |
new Cancion("Como una hoja de papel", "Evelyn Vasquez"), | |
new Cancion("Siempre te amare", "Darwin Lechler"), | |
new Cancion("Brilla", "Darwin Lechler"), | |
new Cancion("Sin amor", "Carlos & Carito"), | |
new Cancion("Te amare", "Joan Sanchez"), | |
new Cancion("Cuanto me ama", "Orlando Elizalde") | |
)); | |
/**** Iterando ****/ | |
//Antes de java 8 | |
for(Cancion cancion : filtrarCantanteJava7(canciones, "Jon Carlo")){ | |
System.out.println("Java 7: "+cancion); | |
} | |
//Java 8 | |
List<Cancion> listadoCanciones = filtrarCantanteJava8(canciones,"Jon Carlo"); | |
listadoCanciones.forEach(c -> System.out.println("Java 8: "+c)); | |
/**** Fin iterando ***/ | |
obtenerCancionesPorCantanteJava8(canciones, "Orlando Elizalde").forEach(System.out::println); | |
//Aplicando un distinct | |
canciones.add(new Cancion("Siempre te amare", "Darwin Lechler")); | |
canciones.stream().distinct().collect(Collectors.toList()).forEach( System.out::println); | |
long count = canciones.stream().filter(c -> c.getCantante().equalsIgnoreCase("Jon Carlo")).count(); | |
System.out.println("Jon Carlo aparece: "+count+" veces"); | |
Map<String, Long> counted = canciones.stream().collect( | |
Collectors.groupingBy( c -> c.getCantante(), Collectors.counting()) | |
); | |
System.out.println(counted); | |
} | |
public List<Cancion> filtrarCantanteJava7(List<Cancion> canciones, String cantante){ | |
List<Cancion> listaFiltrada = new LinkedList<>(); | |
for(Cancion cancion : canciones){ | |
if(cancion.getCantante().equals(cantante)) | |
listaFiltrada.add(cancion); | |
} | |
return listaFiltrada; | |
} | |
public List<Cancion> filtrarCantanteJava8(List<Cancion> canciones, String cantante){ | |
return canciones.stream() | |
.filter(c -> c.getCantante().equals(cantante)) | |
.map( c -> { | |
c.setCantante(c.getCantante().toUpperCase()); | |
return c; | |
}) | |
.collect(Collectors.toList()); | |
} | |
public List<String> obtenerCancionesPorCantanteJava8(List<Cancion> canciones, String cantante){ | |
return canciones.stream() | |
.filter(c -> c.getCantante().equals(cantante)) | |
.map(c -> c.getNombre().toUpperCase()) | |
.collect(Collectors.toList()); | |
} | |
public static void main(String[] args) { | |
new StreamJava8(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment