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<String> list = new ArrayList<>(); list.add("wudang"); list.add("emei"); list.add("mingjiao"); Stream stream3 = list.stream(); //集合 Set<String> set = new HashSet<>(); set.add("shaolin"); set.add("kongtong"); Stream stream4 = set.stream(); //map Map<String, Object> 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<String> strings = (List<String>) stream3.collect(Collectors.toList()); System.out.println(strings); //集合 Set<String> set1 = (Set<String>) stream4.collect(Collectors.toSet()); System.out.println(set1); //map Map<String, String> map1 = (Map<String, String>) stream5.collect(Collectors.toMap(x -> x, y -> y)); System.out.println(map1);
2.Stream对象常见API操作
//4.Stream中常见的API操作 List<String> 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<Integer> 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());