Java 8: Streams & Lambdas

package com.codemonkey; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.Random; public class Main { public static void main(String[] args) { List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList()); log(""+strings); log(""+filtered); Random aleatorio = new Random(); aleatorio.ints().limit(10).forEach(System.out::println); List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5); List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList()); log("numbers: "+numbers); log("squaresList: "+squaresList); } static void log(String out){ System.out.println(out); } }
Ejemplo de uso de Streams y Lambdas

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.