java stream lambda_java8 stream lambda

记录Java8的stream操作,供自己复习。

创建Stream

Employee类

class Employee{

public int id;

public String name;

public double salary;

public Employee(int id, String name, double salary){

this.id = id;

this.name = name;

this.salary = salary;

}

@Override

public String toString() {

return "Employee{" +

"id=" + id +

", name='" + name + '\'' +

", salary=" + salary +

'}';

}

public void salaryIncrement(double add){

this.salary += add;

}

}

创建stream方法

Employee[] arrayOfEms = {

new Employee(1, "Jeff Bezos", 100000.0),

new Employee(2, "Bill Gates", 200000.0),

new Employee(3, "Mark Zuckerberg", 300000.0)

};

//1

Stream.of(arrayOfEms);

//2

List empList = Arrays.asList(arrayOfEms);

empList.stream();

//3

Stream.of(arrayOfEms[0], arrayOfEms[1], arrayOfEms[2]);

//4

Stream.Builder employeeBuilder = Stream.builder();

employeeBuilder.accept(arrayOfEms[0]);

employeeBuilder.accept(arrayOfEms[1]);

employeeBuilder.accept(arrayOfEms[2]);

Stream empStream = employeeBuilder.build();

forEach

forEach就是对stream进行遍历,但是不保证遍历的顺序。forEach是一个终端操作,流管道被视为已消耗,不能在使用。所以在forEach后不再跟其他操作。

empList.stream().forEach(e -> e.salaryIncrement(10.0));

empList.stream().forEach(e -> System.out.println(e));

map

map和Python里面的意思一样,就是将每个元素应用同样的操作。根据empIds里面的id将对应的元素找出来。

class employeeRepository{

public static Employee findById(int id){

Employee[] arrayOfEms = {

new Employee(1, "Jeff Bezos", 100000.0),

new Employee(2, "Bill Gates", 200000.0),

new Employee(3, "Mark Zuckerberg", 300000.0)

};

List employees = Arrays.asList(arrayOfEms).stream().filter(e -> e.getId() == id).collect(Collectors.toList());

if(employees.size() > 0)

return employees.get(0);

else

return null;

}

}

Integer[] empIds = {1, 3};

List employees = Stream.of(empIds)

.map(employeeRepository::findById)

.collect(Collectors.toList());

employees.forEach(e -> System.out.println(e));

collect

就像前面的map处理通过collect操作获得List结果一样,collect将流管道中的元素重新组合起来。

List employees = empList.stream().collect(Collectors.toList());

employees.forEach(e -> System.out.println(e));

filter

就是对stream中的元素进行过滤。下面的操作表示先使用map选出empIds中的元素,然后在将工资小于200000的过滤掉。

Integer[] empIds = {1, 2, 3, 4};

List employees = Stream.of(empIds)

.map(employeeRepository::findById)

.filter(e -> e != null)

.filter(e -> e.getSalary() > 200000)

.collect(Collectors.toList());

employees.forEach(e -> System.out.println(e));

findFirst

顾名思义,找到第一个符合条件的数据。在指定id中找到第一个工资大于100000的元素。

Integer[] empIds = {1, 2, 3, 4};

Employee employee = Stream.of(empIds)

.map(employeeRepository::findById)

.filter(e -> e != null)

.filter(e -> e.getSalary() > 100000)

.findFirst()

.orElse(null);

System.out.println(employee);

toArray

将管道流中的元素转变为数组,提供了一种列表转数组的方法,其中Employee[]::new表示创建一个空数组,用流中的元素将其填满。

Employee[] employees = empList.stream().toArray(Employee[]::new);

System.out.println(Arrays.toString(employees));

flatMap

将复杂的数据结构拉平

List> namesNested = Arrays.asList(

Arrays.asList("Jeff", "Bezos"),

Arrays.asList("Bill", "Gates"),

Arrays.asList("Mark", "Zuckerberg")

);

List namesFlatStream = namesNested.stream()

.flatMap(Collection::stream)

.collect(Collectors.toList());

System.out.println(namesFlatStream);

peek

前面已经说了forEach是个终端操作,也就是后面不能再有其他操作,但是我想对一个元素多次操作怎么办呢?peek可以对流中的元素执行指定的操作,然后返回一个可以继续使用的新流,peek是一个中间操作。

Employee[] arrayOfEmps = {

new Employee(1, "Jeff Bezos", 1000.0),

new Employee(2, "bill Gates", 2000.0),

new Employee(3, "Mark Zuckerberg", 3000.0)

};

List empList = Arrays.asList(arrayOfEmps);

empList.stream()

.peek(e -> e.salaryIncrement(10.0))

.peek(System.out::println)

.collect(Collectors.toList());

System.out.println(empList);

Method types and Pipelines

正如前面所说的,流操作分为中间操作和终端操作,例如filter之类的中间操作返回一个新的流,可以对其做进一步的处理,终端操作例如forEach将流标记为已使用,此后无法在对其进行任何操作。

流管道包括一个stream source、多个中间操作以及一个终端操作。

long empCount = empList.stream()

.filter(e -> e.getSalary() > 200000.0)

.count();

System.out.println("empCount " + empCount);

Stream infiniteStream = Stream.iterate(2, i -> i*2);

List collect = infiniteStream.skip(3)

.limit(5)

.collect(Collectors.toList());

System.out.println("collect " + collect);

惰性计算

流的重要特征之一是可以通过惰性计算进行重要优化。仅在启动终端操作的时候才会对源数据进行计算,并且仅在需要的时候才使用源元素。所有的中间操作都是惰性计算,因此只有在实际需要处理结果的时候它们才会执行。

想一想之前的findFirst例子,map操作一共执行了几次?是4次吗?因为我们的输入empIds数组长度为4。

Integer[] empIds = {1, 2, 3, 4};

Employee employee = Stream.of(empIds)

.map(employeeRepository::findById)

.filter(e -> e != null)

.filter(e -> e.getSalary() > 100000)

.findFirst()

.orElse(null);

System.out.println(employee);

一个元素一次执行一个map和两个filter操作。它首先对id 1执行所有操作。由于id 1的薪水不大于100000,因此处理移至下一个元素。id 2满足两个filter,因此流终端操作findFirst()执行并返回结果。id 3和id 4没有执行任何操作。懒惰处理数据流可以避免在不必要时检查所有数据。当输入流是无限或者很大时,这种特点就更加重要。

基于流的比较操作

sorted

流元素的比较基于我们传递进去的比较器

List employees = empList.stream()

.sorted((e1, e2) -> e1.getName().compareTo(e2.getName()))

.collect(Collectors.toList());

System.out.println(employees);

min & max

顾名思义,返回流中的最大值或者最小值基于我们传递进去的比较器。返回结果是optional,因为可能存在可能不存在(例如由于filter)

Employee firstEmp = empList.stream()

.min((e1, e2) -> e1.getId() - e2.getId())

.orElseThrow(NoSuchElementException::new);

System.out.println(firstEmp);

我们还可以使用Comparator.comparing定义比较器

Employee maxSalEmp = empList.stream()

.max(Comparator.comparing(Employee::getSalary))

.orElseThrow(NoSuchElementException::new);

System.out.println(maxSalEmp);

distinct

distinct不需要传递参数,返回流中不同的元素,它使用元素的equals判断元素是否相等

List intList = Arrays.asList(2,5,3,2,4,3);

List distinctIntList = intList.stream()

.distinct()

.collect(Collectors.toList());

System.out.println(distinctIntList);

match

allMatch都要符合,anyMatch有一个符合即可,noneMatch都不能符合。

List intList = Arrays.asList(2,4,5,6,8);

boolean allEven = intList.stream().allMatch(i -> i%2 == 0);

boolean oneEven = intList.stream().anyMatch(i -> i%2 == 0);

boolean noneMultipleOfThree = intList.stream().noneMatch(i -> i%3 == 0);

System.out.println("allEven: " + allEven + " oneEven: " + oneEven + " noneMultipleOfThree: " + noneMultipleOfThree);

stream specializations(流定制化,不知道这么翻译对不对)

根据到目前为止的讨论,Stream是对象引用的流。但是,还有IntStream,LongStream和DoubleStream –它们分别是int,long和double的重要定制。这些在处理许多数值型元素时非常方便。

这些定制流并不是扩展了Stream而是扩展了BaseStrean

92301068ce6b

stream

因此,并非所有Stream支持的操作都出现在这些Stream实现中。例如,标准min()和max()使用比较器,而专用流则不需要。

creation

最常用的创建IntStream流的方法就是对现有流使用mapToInt()。使用mapToInt创建了IntStream,然后使用max找出最大的id。

Integer latestEmpId = empList.stream()

.mapToInt(Employee::getId)

.max()

.orElseThrow(NoSuchElementException::new);

System.out.println("latestEmpId: " + latestEmpId);

还可以使用IntStream.of(1,2,3)和IntStream.range(10,20)进行创建

在进行下面的学习之前需要注意一个重要的区别。Stream.of(1,2,3)返回的不是IntStream而是Stream。同样地使用map而不是mapToInt返回的也不是IntStream,empList.stream().map(Employee::getId);返回的是Stream。

Specialized Operations

先比较一般的流,这些定制流可以很方面的进行数值型的操作,比如sum()、average()、range(),因为都是数值嘛。

Double avgSal = empList.stream()

.mapToDouble(Employee::getSalary)

.average()

.orElseThrow(NoSuchElementException::new);

System.out.println("avgSal: " + avgSal);

Reduction Operations

学过Python的应该对reduce操作很熟悉,就是将第一个元素与第二个元素按照指定操作进行处理,然后将处理结果与第三个元素进行处理,以此类推直到最后一个元素,前面讲的findFirst(), min() and max()都是这样的操作。对empList中的薪资进行求和,第一个操作数是0.0然后后面一直执行sum操作,直到最后一个元素。

Double sumVal = empList.stream()

.map(Employee::getSalary)

.reduce(0.0, Double::sum);

System.out.println("sumVal: " + sumVal);

高级collect

joining

String empNames = empList.stream()

.map(Employee::getName)

.collect(Collectors.joining(", "))

.toString();

System.out.println(empNames);

toSet

Set empNames = empList.stream()

.map(Employee::getName)

.collect(Collectors.toSet());

System.out.println(empNames);

toCollection

Vector empNames = empList.stream()

.map(Employee::getName)

.collect(Collectors.toCollection(Vector::new));

System.out.println(empNames);

summarizingDouble

有点像pandas的summary,给出count、min、max、average等统计信息

DoubleSummaryStatistics stats = empList.stream()

.collect(Collectors.summarizingDouble(Employee::getSalary));

stats = empList.stream()

.mapToDouble(Employee::getSalary)

.summaryStatistics();

System.out.println("getCount: " + stats.getCount()

+"\ngetSum: " + stats.getSum() + "\ngetMin: " + stats.getMin()

+"\ngetMax: " + stats.getMax() + "\ngetAverage: " + stats.getAverage());

partitioningBy

对流中的元素基于某个条件判断进行分区

List intList = Arrays.asList(2,4,5,6,8);

Map> isEven = intList.stream()

.collect(Collectors.partitioningBy(i -> i % 2 == 0));

System.out.println(isEven.get(true));

System.out.println(isEven.get(false));

groupingBy

如果学习过SQL语句就很好理解了,是一种更高级的分区,可以将流中元素分成不止两个分区。它采用一个分类函数作为参数,这个分类函数被应用到流中每一个元素。这个分类函数的返回值就是Map的key。根据Employee的首字母将列表中的元素分组。

Map> groupByAlphabet = empList.stream()

.collect(Collectors.groupingBy(e -> new Character(e.getName().charAt(0))));

for(HashMap.Entry> item:groupByAlphabet.entrySet())

System.out.println(item.getKey() + ": " + item.getValue());

mapping

对groupingBy的结果再进行抽取处理,直接看例子可能更好一些。

Map> idGroupedByAlphabet = empList.stream()

.collect(Collectors.groupingBy(e -> new Character(e.getName().charAt(0)),

Collectors.mapping(Employee::getId, Collectors.toList())));

for(HashMap.Entry> item:idGroupedByAlphabet.entrySet())

System.out.println(item.getKey() + ": " + item.getValue());

reducing

下面是两个例子,第二个操作是找到每个分组的最长的名字。

Double percentage = 10.0;

Double salIncrOverhead = empList.stream()

.collect(Collectors.reducing(0.0, e -> e.getSalary()*percentage/100,

(s1,s2) -> s1+s2));

System.out.println(salIncrOverhead);

Comparator byNameLength = Comparator.comparing(Employee::getName);

Map> longestNameByAlphabet = empList.stream()

.collect(Collectors.groupingBy(e -> new Character(e.getName().charAt(0)),

Collectors.reducing(BinaryOperator.maxBy(byNameLength))));

for(HashMap.Entry> item:longestNameByAlphabet.entrySet())

System.out.println(item.getKey() + ": " + item.getValue());

并行流

只需要加上parallel即可

Employee[] arrayOfEmps = {

new Employee(1, "Jeff Bezos", 100000.0),

new Employee(2, "Bill Gates", 200000.0),

new Employee(3, "Mark Zuckerberg", 300000.0)

};

List empList = Arrays.asList(arrayOfEmps);

empList.stream().parallel().forEach(e -> e.salaryIncrement(10.0));

System.out.println(empList);

无限流

Stream.generate(Math::random)

.limit(5)

.forEach(System.out::println);

Stream evenNumStream = Stream.iterate(2, i -> i*2);

List collect = evenNumStream

.limit(5)

.collect(Collectors.toList());

System.out.println(collect);

文件操作

String[] words = {

"heelo",

"refer",

"world",

"level"

};

try(PrintWriter pw = new PrintWriter(

Files.newBufferedWriter(Paths.get("./file.txt")))){

Stream.of(words).forEach(pw::println);

} catch (IOException e) {

e.printStackTrace();

}

List str = null;

try {

str = Files.lines(Paths.get("./file.txt"))

.filter(s -> s.length() == 5)

.filter(s -> s.compareToIgnoreCase(new StringBuffer(s).reverse().toString()) == 0)

.collect(Collectors.toList());

} catch (IOException e) {

e.printStackTrace();

}

System.out.println(str);

stream在Java9中的改进

takeWhile

当流中的元素符合(while)某个条件的时候,就把元素取走(take)。

Stream.iterate(1, i -> i + 1)

.takeWhile(n -> n <= 10)

.map(x -> x * x)

.forEach(System.out::println);

这个takeWhile看着和filter很类似,区别是什么?下面的例子很清楚了说明了区别,takeWhile在遇到条件不符合的时候就终止了,而filter会处理每一个元素。

Stream.of(1,2,3,4,5,6,7,8,9,0,9,8,7,6,5,4,3,2,1,0)

.takeWhile(x -> x <= 5)

.forEach(System.out::println);

# 输出

1

2

3

4

5

Stream.of(1,2,3,4,5,6,7,8,9,0,9,8,7,6,5,4,3,2,1,0)

.filter(x -> x <= 5)

.forEach(System.out::println);

# 输出

1

2

3

4

5

0

5

4

3

2

1

dropWhile

与takeWhile类似,只是作用相反。

iterate

java8中iterate是个无限流,没有终止条件。Java9中增加了一个判断条件。

Stream.

iterate(1, i -> i < 256, i -> i * 2)

.forEach(System.out::println);

与下面for循环作用一样。

for (int i = 1; i < 256; i*=2) {

System.out.println(i);

}

ofNullable

Stream result = number != null

? Stream.of(number)

: Stream.empty();

上面的number可能来源于网络、前端或者其他不信任的地方。所以它可能是null,我们不想创建一个null的流,所以需要对number进行判断,然后返回一个empty的流。

但是上面这个例子是经过精心设计的,时间情况可能非常复杂,需要进行判断处理。这个时候可以利用ofNullable进行处理。

Stream result = Stream.ofNullable(number);

ofNullable当接收number为null时候,可以返回空的optional,必须程序报错。就行下面代码所示。

Integer number = null;

Stream result = Stream.ofNullable(number);

result.map(x -> x * x).forEach(System.out::println);

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值