jdk8 Lambda表达式Stream

jdk8 Lambda表达式Stream
函数式接口(Functional Interface)就是一个唯一的抽象方法,但是可以有多个非抽象方法发的接口可以被隐式转换成Lambda表达式。
1、list转map
Collectors.toMap
2、foreach输出

List<String> uerName = Array.asList("A","B","C");
userName.forEach(System.out::print);
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put("","");
...
hashMap.forEach((k,v)->System.out.println(k + ":\t" + v));

3、groupby分组

Map<String,List<UserInfo>> result = originUserInfoList
	.stream()
	.collect(Collectors.groupingBy(UserInfo::getCity));

4、Filter过滤不符合条件的

List<UserInfo> userInfo = userInfo
	.stream()
	.filter(user -> user.getAge() > 50)
	.collect(Collectors.toList());
userInfo.forEach(a -> System.out.println(a.getUserName()));

5、Comparator排序

List<UserInfo> userInfo = new ArrayList<>();
userInfo = userInfo
	.stream()
	.sorted(Comparator.comparing(UserInfo::getAge))
	.reversed()
	.collect(Collectors.toList());
userInfo.forEach(a -> System.out.println(a.toString()));

6、去除重复元素
.distinct()
7、findFirst()返回第一个

List<String> userName = Array.asList("A","B","C");
List.stream()
	.findFirst()
	.ifPresent(System.out::println);

8、anyMatch是否至少匹配一个元素,allMatch是否都满足给定元素

Stream<String> stream = stream.of("A","B","C");
Boolean match = stream.anyMatch(s -> s.contains("C"));//输出:true
Boolean match = stream.allMatch(s -> s.contains("C"));//输出:false

9、map方法进行元素转换

List<String> upperCaseList = list.stream()
								.map(String::toUpper)
								.collect(Collectors.toList());
upperCaseList.forEach(System.out::Println);

10、Reduce合并流的元素

System.out.println(Stream.of(1,2,3,4).reduce(0,(a,b) -> a + b));

11、peek打印日志

List<String> res = Stream.of("Aa","Ba","Cc")
						.filter(a -> a.contains("a")
						.peek(a -> System.out.println(a))
						.collect(Collectors.toList());
//输出
Aa
Ba
[Aa,Ba]

12、max & min & count

List<UserInfo> userInfoList = new ArrayList<>();
userInfoList.add("...");
...
Optional<UserInfo> maxAge = userInfoList.stream()
										.max(Comparator.comparing(UserInfo::getAge));
System.out.println(userInfoList.stream()
								.filter(user -> user.getAge() > 60)
								.count());
//输出
//{userId = 2,userName = "",age = 27}

13、函数式接口
Function<T,R>(转换型):input value,return result

Fuction<String,Integer> function = String::length;//获取字符串长度
Stream<String> stream = Stream.of("AAA","BBB");
Stream<Integer> resultStream = stream.map(function);
resultStream.forEach(System.out::println);

Consumer(消费型):input value,no return

Consumer<String> consumer = String.out::println;//获取字符串长度
Stream<String> stream = Stream.of("AAA","BBB");
stream.forEach(consumer);

Predicate(判断型):input value,return Boolean result

Predicate<Integer> predicate = a -> a > 50;//获取字符串长度
UserInfo user = new UserInfo(2L,"A",25);
System.out.println(predicate.test(user.getAge()));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值