大丰网站开发,长沙官网seo分析,培训页面设计师,市场调研问卷Stream流 创建流中间操作1、filter2、map3、distinct4、sorted5、limit6、skip7、flatMap 终结操作1、forEach2、count3、maxmin4、collect5、查找与匹配 创建流
单例集合#xff1a;集合对象.stream()
ListInteger list new ArrayList();
Stream… Stream流 创建流中间操作1、filter2、map3、distinct4、sorted5、limit6、skip7、flatMap 终结操作1、forEach2、count3、maxmin4、collect5、查找与匹配 创建流
单例集合集合对象.stream()
ListInteger list new ArrayList();
StreamInteger stream list.stream();数组Arrays.stream(数组) 或 Stream.of(数组) 来创建
Integer[] arr {1,2,3,4};
StreamInteger stream1 Arrays.stream(arr);
StreamInteger stream2 Stream.of(arr);双例集合转换成单例集合后再创建
MapString, String map new HashMap();
StreamMap.EntryString, String stream3 map.entrySet().stream();中间操作
1、filter
对流中的元素进行条件过滤符合过滤条件的才能继续留在流中。
public static void test() {ListAuthor authors getAuthors();authors.stream().filter(author - author.getAge() 18) //中间操作.forEach(author - System.out.println(author)); //终结操作
}2、map
可以把流中的元素进行计算或转换。
转换
public static void test() {ListAuthor authors getAuthors();//泛型中第一个参数为方法的参数类型第二个参数为方法的返回值类型authors.stream().map(author - author.getName()).forEach(name - System.out.println(name));
}3、distinct
去除流中的重复元素。
注意distinct方法是依赖Object的equals方法来判断是否是相同对象所以需要重写equals方法。
4、sorted
对流中的元素进行排序。
方式一调用sorted()空参方法 在比较的实体类上要实现Comparable接口不然会报类型不匹配的异常。 public static void test2() {ListAuthor authors getAuthors();authors.stream().distinct().sorted().forEach(author - System.out.println(author.getAge()));
}方式二在sorted方法中实现Comparator接口
public static void test2() {ListAuthor authors getAuthors();authors.stream().distinct().sorted(new ComparatorAuthor() {Overridepublic int compare(Author o1, Author o2) {return o1.getAge()-o2.getAge();}}).forEach(author - System.out.println(author.getAge()));
}优化
public static void test2() {ListAuthor authors getAuthors();authors.stream().distinct().sorted((o1, o2) - o1.getAge()-o2.getAge()).forEach(author - System.out.println(author.getAge()));
}5、limit
可以设置流的最大长度超出的部分将被抛弃。
//对流中的元素按照年龄进行降序排序并且要求不能有重复元素打印其中年龄最大的两个作家。
public static void test2() {ListAuthor authors getAuthors();authors.stream().distinct().sorted((o1, o2) - o2.getAge()-o1.getAge()).limit(2).forEach(author - System.out.println(author.getName()));
}6、skip
跳过流中的前n个元素返回剩下的元素。
7、flatMap
map只能把一个对象转换成另一个对象来作为流中的元素。而flatMap可以把一个对象转换成多个对象作为流中的元素。
案例1打印所有书籍的名字要求对重复的元素进行去重。 map方式Author对象的books属性是集合类型使用原来map转换对象要使用嵌套循环进行打印。
public static void test2() {ListAuthor authors getAuthors();authors.stream().map(author - author.getBooks()).forEach(books - {for (Book book : books) {System.out.println(book.getName());}});
}flatMap方式
public static void test2() {ListAuthor authors getAuthors();authors.stream().flatMap(author - author.getBooks().stream()).forEach(book - System.out.println(book.getName()));
}案例二打印现有数据的所有分类要求对分类进行去重。不能出现这种格式哲学,爱情要将它们拆开输出。
public static void test3() {ListAuthor authors getAuthors();authors.stream().flatMap(author - author.getBooks().stream()).distinct().flatMap(book - Arrays.stream(book.getCategory().split(,))).distinct().forEach(category - System.out.println(category));
}
终结操作
1、forEach
对流中的元素进行遍历操作我们通过传入的参数去指定对遍历到的元素进行什么具体操作。
2、count
获取当前流中元素的个数。
//打印这些作家的所出书籍的数量
public static void test4() {ListAuthor authors getAuthors();long count authors.stream().flatMap(author - author.getBooks().stream()).distinct().count();System.out.println(count);
}
3、maxmin
获取流中的最值
//分别获取这些作家所出书籍的最高分和最低分
public static void test4() {ListAuthor authors getAuthors();OptionalInteger max authors.stream().flatMap(author - author.getBooks().stream()).map(book - book.getScore()).max((o1, o2) - o1 - o2);OptionalInteger min authors.stream().flatMap(author - author.getBooks().stream()).map(book - book.getScore()).min(((o1, o2) - o1 - o2));System.out.println(max.get());System.out.println(min.get());
}4、collect
把当前流转换成一个集合。
list集合
//获取一个存放所有作者名字的list集合
public static void test5() {ListAuthor authors getAuthors();ListString nameList authors.stream().map(author - author.getName()).collect(Collectors.toList());System.out.println(nameList);
}
set集合
//获取一个存放所有作者名字的set集合
public static void test5() {ListAuthor authors getAuthors();SetString nameList authors.stream().map(author - author.getName()).collect(Collectors.toSet());System.out.println(nameList);
}
map集合
//获取一个Map集合map的key为作者名value为ListBook
public static void test5() {ListAuthor authors getAuthors();MapString, ListBook map authors.stream().distinct().collect(Collectors.toMap(author - author.getName(), author - author.getBooks()));System.out.println(map);
}
5、查找与匹配
1anyMatch判断是否有任意符合匹配条件的元素结果为boolean类型。
//判断是否有作家年龄在18以上
public static void test6() {ListAuthor authors getAuthors();boolean flag authors.stream().anyMatch(author - author.getAge() 18);System.out.println(flag);
}2allMatch判断是否都符合条件如果都符合返回true否则返回false
//判断是否所有作家年龄在18以上
public static void test6() {ListAuthor authors getAuthors();boolean flag authors.stream().allMatch(author - author.getAge() 18);System.out.println(flag);
}
3noneMatch判断流中的元素是否都不符合匹配条件如果都不符合结果为true否则结果为false。
4findAny获取流中的任意一个元素。该方法没有办法保证获取到的一定是流中的第一个元素。
//获取任意一个年龄大于18的作家如果存在就输出他的名字
public static void test7() {ListAuthor authors getAuthors();OptionalAuthor any authors.stream().filter(author - author.getAge() 18).findAny();//如果这个Optional中有元素则执行方法没有就不执行any.ifPresent(author - System.out.println(author.getName()));
}
5findFirst获取流中的第一个元素。
//获取一个年龄最小的作家并输出他的姓名
public static void test7() {ListAuthor authors getAuthors();OptionalAuthor any authors.stream().sorted((o1, o2) - o1.getAge()-o2.getAge()).findFirst();any.ifPresent(author - System.out.println(author.getName()));
}6reduce归并 对流中的数据按照你指定的计算方式计算出一个结果。缩减操作
reduce的作用是把stream中的元素给组合起来。我们可以传入一个初始值它会按照我们的计算方式依次拿流中的元素和初始化值进行计算计算结果再和后面的元素计算。
它内部的计算方式如下
T result identity;
for (T element : this stream)result accumulator.apply(result, element)
return result;
其中identity就是我们可以通过方法参数传入的初始值accumulator的apply具体进行什么计算也是我们通过方法参数来确定的。
案例1使用reduce求所有作者年龄的和
public static void test8() {ListAuthor authors getAuthors();Integer sum authors.stream().distinct().map(author - author.getAge())//初始值为0后面 resultelement最后 return result.reduce(0, (result, element) - result element);System.out.println(sum);
}案例2使用reduce求所有作者中年龄的最大值
public static void test8() {ListAuthor authors getAuthors();Integer max authors.stream().map(author - author.getAge()).reduce(Integer.MIN_VALUE, (result, element) - result element ? result : element);System.out.println(max);
}reduce有个重载形式内部代码如下
boolean foundAny false;
T result null;
for (T element : this stream) {if(!foundAny) {foundAny true;result element;} else {result accumulator.apply(result, element);}
}
return foundAny ? Optional.of(result) : Optional.empty();
利用这个重载形式求作者年龄的最大值不用传递初始值了。
public static void test8() {ListAuthor authors getAuthors();OptionalInteger max authors.stream().map(author - author.getAge()).reduce((result, element) - result element ? result : element);System.out.println(max.get());
}注意事项 惰性求值如果没有终结操作中间操作是不会得到执行的。
流是一次性的一旦一个流对象经过一个终结操作后这个流就不能再被使用了只能重新创建流对象再使用。
不会影响原数据我们在流中可以对数据做很多处理但正常情况下是不会影响原来集合中的元素的。