1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| package com.example.demo;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamMethod {
//提取集合中所有偶数并求和
@Test
public void case1() {
//List<>不加String为一个对象
List<String> list = Arrays.asList("1", "2", "3", "4", "5", "6");
int sum = list.stream()//获取stream对象
.mapToInt(s -> Integer.parseInt(s))//mapToInt将流中每一个数据转为整数
.filter(n -> n % 2 == 0) //filter对流数据进行过滤
.sum();//求和
System.out.println(sum);
}
//所有名字首字母大写
@Test
public void case2() {
List<String> list = Arrays.asList("Lily", "smith", "jackson");
List<String> newList = list.stream()
//按规则对每一个流数据进行转换
.map(s -> s.substring(0, 1).toUpperCase() + s.substring(1))
//.forEach(s -> System.out.println(s)) //打印结果
//collect对流数据进行收集。生成新的List/Set
.collect(Collectors.toList());
System.out.println(newList);
}
//将所有奇数从大到小进行排序,且不许出现重复
@Test
public void case3() {
List<Integer> list = Arrays.asList(1, 60, 38, 21, 51, 60, 51, 73);
List newList = list.stream().distinct()//去除重复的流数据
.filter(n -> n % 2 == 1)
.sorted((a, b) -> b - a) //流数据排序
.collect(Collectors.toList());//从大到小
System.out.println(newList);
}
} |
共 0 条评论关于"Stream常用方法"
最新评论