streamset 数据合并_java8新特性-Stream操作集合中的数据

1.类型转换

(1)其他类型转换成Stream对象

public class App {

public static void main(String[] args) {

//1.批量数据 --> Stream对象

//多个数据

Stream stream1 = Stream.of("admin", "tom", "mike");

//数组

String[] array = new String[]{"xuexi", "biyao"};

Stream stream2 = Arrays.stream(array);

//列表

List list = new ArrayList<>();

list.add("wudang");

list.add("emei");

list.add("mingjiao");

Stream stream3 = list.stream();

//集合

Set set = new HashSet<>();

set.add("shaolin");

set.add("kongtong");

Stream stream4 = set.stream();

//map

Map map = new HashMap<>();

map.put("张三", 11);

map.put("李四", 15);

map.put("王五", 16);

Stream stream5 = map.entrySet().stream();

//2.Stream对象对于基本数据类型的功能封装

// int/long/double

IntStream.of(new int[]{10, 20, 30}).forEach(System.out::println);

IntStream.range(1, 5).forEach(System.out::println);

IntStream.rangeClosed(1, 5).forEach(System.out::println);

}

}

(2)Stream对象转换成其他对象

//3.Stream转换成指定数据对象

//数组

Object[] object = stream1.toArray(String[]::new);

//字符串

String s = stream2.collect(Collectors.joining()).toString();

System.out.println(s);

//列表

List strings = (List) stream3.collect(Collectors.toList());

System.out.println(strings);

//集合

Set set1 = (Set) stream4.collect(Collectors.toSet());

System.out.println(set1);

//map

Map map1 = (Map) stream5.collect(Collectors.toMap(x -> x, y -> y));

System.out.println(map1);

2.Stream对象常见API操作

//4.Stream中常见的API操作

List accountList = new ArrayList<>();

accountList.add("songjiang");

accountList.add("wuyong");

accountList.add("lujunyi");

accountList.add("linchong");

//map()中间操作,map()方法接收一个Function接口

accountList = accountList.stream().map(x -> "梁山好汉:" + x).collect(Collectors.toList());

//添加过滤条件,过滤符合条件的用户 filter()

accountList = accountList.stream().filter(x -> x.length() > 7).collect(Collectors.toList());

// forEach 增强型循环

accountList.forEach(x -> System.out.println("forEach:" + x));

// peek() 迭代数据完成数据的依次处理过程

accountList.stream().peek(x -> System.out.println("peek 1:" + x))

.peek(x -> System.out.println("peek 2 :" + x)).forEach(System.out::println);

accountList.forEach(System.out::println);

//Stream中对于数字的操作

List intList = new ArrayList<>();

intList.add(20);

intList.add(14);

intList.add(11);

intList.add(12);

intList.add(10);

intList.add(18);

intList.add(19);

intList.add(12);

//skip()中间操作,有状态,跳过部分数据

intList.stream().skip(3).forEach(System.out::println);

//limit()限制输出的数量

intList.stream().skip(3).limit(3).forEach(System.out::println);

System.out.println("-------------------分割线-----------------------");

//distinct() 中间操作,有状态,剔除重复数据

intList.stream().distinct().forEach(System.out::println);

System.out.println("-------------------分割线-----------------------");

//sorted() 中间操作,有状态,排序

//max() 获取最大操作

//min() 获取最小值

//reduce() 合并处理数据

Optional optional = intList.stream().max((x, y) -> x - y);

System.out.println(optional.get());

Optional optional1 = intList.stream().reduce((sum, x) -> sum + x);

System.out.println(optional1.get());

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值