I have learnt about Stream API. Do you have any practice for me? This is also implement lambda expression and what I have to study?(I learn by myself)
import java.util.List;;
class Demo {
public static String title(String name) {
String prefix = name.contains("a") ? "Mrs. " : "Mr. ";
return prefix + name;
}
public static void main(String[] args) {
List<Integer> list = List.of(5, 3, 1, 4, 2, 6);
List<String> names = List.of("John", "Jane", "Jack", "Jill", "Yo", "Dol", "Boss");
// Stream<Integer> test = list.stream();
// Stream<Integer> mappedTest = test.map(n -> 2 + n);
list.stream().filter(n -> n % 2 == 1).sorted().map(n -> 2 * n).forEach(n -> System.out.println(n));
//list.stream().sorted().map(n -> n + 2).filter(n -> n % 2 == 0).forEach(n -> System.out.println(n));
// names.stream().filter(n -> n.charAt(1) != 'o').map(n -> title(n)).forEach(n
// -> System.out.println(n));
// test.forEach(n -> System.out.println(2 * n));
// mappedTest.forEach(n -> System.out.println(n));
}
}
I want some exercise if you have
New contributor
user789994552177 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.