public class TestStream {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(1);
list.add(1);
list.add(1);
List<Integer> result = list.stream()
.mapToInt(x -> x)
.map(x -> ++x)
.boxed()
.collect(Collectors.toCollection(ArrayList::new));
System.out.println(result);
}
}
过滤
public class TestStream {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(8);
list.add(0);
list.add(2);
list.add(1);
List<Integer> result = list.stream()
.mapToInt(x -> x)
.filter(x -> x>2)
.boxed()
.collect(Collectors.toCollection(ArrayList::new));
System.out.println(result);
}
}
自然排序
public class TestStream {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(8);
list.add(0);
list.add(2);
list.add(1);
List<Integer> result = list.stream()
.mapToInt(x -> x)
.sorted()
.boxed()
.collect(Collectors.toCollection(ArrayList::new));
System.out.println(result);
}
}
规约统计
public class TestStream {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(8);
list.add(0);
list.add(2);
list.add(1);
int max = list.stream()
.mapToInt(x -> x)
.max()
.getAsInt();
System.out.println(max);
}
}