public class Jdk8Stream { /* *Stream 接口java.util.Stream 表示能应用在一组元素上一次执行的操作序列。 * Stream 操作分为中间操作或者最终操作两种,最终操作返回一特定类型的计算结果,而中间操作返回Stream本身,这样你就可以将多个操作依次串起来。 * Stream 的创建需要指定一个数据源,比如 java.util.Collection的子类,List或者Set, Map不支持。Stream的操作可以串行执行或者并行执行。 *首先看看Stream是怎么用,首先创建实例代码的用到的数据List: */ public static void main(String[] args){ List<String> stringCollection = new ArrayList<>(); stringCollection.add("ddd2"); stringCollection.add("aaa2"); stringCollection.add("bbb1"); stringCollection.add("aaa1"); stringCollection.add("bbb3"); stringCollection.add("ccc"); stringCollection.add("bbb2"); stringCollection.add("ddd1"); //Filter 过滤 mFilter(stringCollection); //Sort 排序 mSort(stringCollection); //map函数 mMap(stringCollection); } /** * 过滤通过一个predicate接口来过滤并只保留符合条件的元素, *该操作属于中间操作,所以我们可以在过滤后的结果来应用其他Stream操作(比如forEach)。 *forEach需要一个函数来对过滤后的元素依次执行。forEach是一个最终操作,所以我们不能在forEach之后来执行其他Stream操作。 */ public static void mFilter( List<String> stringCollection){ System.out.println("mFilter----------------------------------"); stringCollection .stream() //获取stream 之后可以进行一系列操作如(filter,forEach,map等) .filter((s) -> s.startsWith("a"))//filter过滤stream 属于一个中间操作,不对结果进行保存 .forEach(System.out::println); //System.out:println jdk1.8 lambda表达式写法相当于forEach(x->System.out.println(x)); //对比 System.out.println("----------------------------------"); for(String strings : stringCollection){ if(strings.startsWith("a")){ System.out.println(strings); } } } /** * Sort 排序 *排序是一个中间操作,返回的是排序好后的Stream。如果你不指定一个自定义的Comparator则会使用默认排序。 */ public static void mSort( List<String> stringCollection){ System.out.println("mSort----------------------------"); stringCollection.stream() .sorted() //排序只创建一个排列好顺序的stream,不会影响原有的数据源,排序之后原有数据不会被修改 .forEach(System.out::println); } /**Map 映射 *中间操作map会将元素根据指定的Function接口来依次将元素转成另外的对象,下面的示例展示了将字符串转换为大写字符串。 * 你也可以通过map来讲对象转换成其他类型,map返回的Stream类型是根据你map传递进去的函数的返回值决定的。 */ public static void mMap(List<String> stringCollection){ System.out.println("mMap----------------------------"); stringCollection.stream().forEach(System.out::println); System.out.println("----------------------------"); stringCollection.stream(). map(s -> s.toUpperCase()) //对stream 英文字母设置为大写 ,不存储结果,不改变原集合数据 .forEach(System.out::println); System.out.println("----------------------------"); List<Person> personList = new ArrayList<>(); personList.add(new Person("aa",12)); personList.add(new Person("bb",22)); personList.add(new Person("cc",32)); System.out.println("---------------------------- 0"); personList.stream().forEach(System.out::println); System.out.println("---------------------------- 1"); List list = personList.stream().map(person -> { Person p = new Person(); if(person.getName().equals("aa")){ p.setName("dd"); //修改Stream 中的数据 p.setAge(23); } return p; }).collect(Collectors.toList()); list.stream().forEach(System.out::println); personList.stream().forEach(System.out::println); System.out.println("---------------------------- 2"); List list2 = personList.stream().map(person -> { if(person.getName().equals("aa")){ person.setName("dd"); //修改Stream 中的数据;因为person为引用对象,所以元数据跟着改变即name为aa的元素改为dd } return person; }).collect(Collectors.toList()); list.stream().forEach(System.out::println); personList.stream().forEach(System.out::println); } } class Person{ private String name; private int age; public Person() { } public Person(String name,int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
jdk1.8 学习笔记
最新推荐文章于 2022-07-19 00:40:05 发布