lista de strings
.streams
// {
package streams;
import java.util.*;
import static java.util.stream.Collectors.*;
public class FlatMap {
// }
public static List<String> transform(List<List<String>> collection) {
List<String> newCollection = new ArrayList<>();
for (List<String> subCollection : collection) {
for (String value : subCollection) {
newCollection.add(value);
}
}
return newCollection;
}
// {
}
// }
1- Transformar a cillection no strea
// {
package streams;
import java.util.*;
import static java.util.stream.Collectors.*;
public class FlatMap {
// }
public static List<String> transform(List<List<String>> collection) {
return collection.stream().flatMap(List::stream).collect( toList());
}
// {
}
// }
Tranformar cada elemento do array para um novo stream
comparator
max - object
Comparator comparing:: getAge
// {
package streams;
import java.util.*;
public class MaxAndComparator {
// }
public static Person getOldestPerson(List<Person> people) {
Person oldestPerson = new Person("", 0);
for (Person person : people) {
if (person.getAge() > oldestPerson.getAge()) {
oldestPerson = person;
}
}
return oldestPerson;
}
// {
}
// }
// {
package streams;
import java.util.*;
public class MaxAndComparator {
// }
public static Person getOldestPerson(List<Person> people) {
return people.stream().max( Comparator.comparing(Person::getAge)).orElse(null);
}
// {
}
// }
// {
package streams;
import java.util.*;
public class SumAndReduce {
// }
public static int calculate(List<Integer> numbers) {
int total = 0;
for (int number : numbers) {
total += number;
}
return total;return numbers.stream().reduce(0, (total, number) -> total + number);
}
// {
}
// }
// {
package streams;
import java.util.*;
public class SumAndReduce {
// }
public static int calculate(List<Integer> numbers) {
return total;return numbers.stream().reduce(0, (total, number) -> total + number);
///return numbers.stream().reduce(0, Integer::sum);
}
// {
}
// }
reduce elemento dentidade lambda