Stream中间操作
- 多个中间操作可以连接起来形成一个流水线,除非流水线上触发终止操作,否则中间操作不会执行任何的处理!而在终止操作时一次性全部处理,称为“惰性求值”。
1,筛选与切片
filter(Predicate p) —— 筛选,传递断言型接口实现类,保留满足条件的元素,过滤其他元素
limit(long maxSize) —— 截断流,使其元素不超过指定数量
skip(long n) —— 跳过前n个元素,返回一个扔掉前n个元素的流,流中元素不足n个,则返回一个空流。与limit(n)互补
distinct() —— 去重,通过流中元素的hashCode()和equals()去除重复元素(需重写元素的hashCode和equals方法)
package com.sunstone.stream;
import com.sunstone.lambda.Employee;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class TestStream {
List<Employee> emps = Arrays.asList(
new Employee("小孙", 17, 5555.55),
new Employee("小思", 27, 3333.33),
new Employee("小东", 37, 9999.99),
new Employee("小零", 50, 7777.77),
new Employee("小雪", 7, 8888.88),
new Employee("小豹", 60, 6666.66)
);
@Test
public void test1() {
Stream<Employee> stream = emps.stream()
.filter((e) -> {
System.out.println("Stream API的中间操作");
return e.getAge() > 35;
});
stream.forEach(System.out::println);
}
@Test
public void test2() {
Iterator<Employee> it = emps.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
package com.sunstone.stream;
import com.sunstone.lambda.Employee;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class TestStream {
List<Employee> emps = Arrays.asList(
new Employee("小孙", 17, 5555.55),
new Employee("小思", 27, 3333.33),
new Employee("小东", 37, 9999.99),
new Employee("小零", 50, 7777.77),
new Employee("小雪", 7, 8888.88),
new Employee("小豹", 60, 6666.66)
);
@Test
public void test3() {
emps.stream()
.filter((e) -> {
System.out.println("limit短路");
return e.getSalary() > 5000;
})
.limit(2)
.forEach(System.out::println);
}
@Test
public void test4() {
emps.stream()
.filter((e) -> {
System.out.println("skip短路");
return e.getSalary() > 5000;
})
.skip(2).distinct()
.forEach(System.out::println);
}
}
2,映射
map(Function f) —— 接收一个函数作为参数,该函数会被作用到每一个元素上,并将其映射成一个新元素。
(用于将元素转换成其他形式或提取信息)
flatMap(Function f) —— 接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流。
mapToDouble(ToDoubleFunction f) —— 产生一个新的DoubleStream
mapToInt(ToIntFunction f) —— 产生一个新的IntStream
mapToLong(ToLongFunction f) —— 产生一个新的LongStream
package com.sunstone.stream;
import com.sunstone.lambda.Employee;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class TestStream {
List<Employee> emps = Arrays.asList(
new Employee("小孙", 17, 5555.55),
new Employee("小思", 27, 3333.33),
new Employee("小东", 37, 9999.99),
new Employee("小零", 50, 7777.77),
new Employee("小雪", 7, 8888.88),
new Employee("小豹", 60, 6666.66)
);
@Test
public void test5() {
List<String> list = Arrays.asList("aaa", "bbb", "ccc");
list.stream()
.map((str) -> str.toUpperCase())
.forEach(System.out::println);
emps.stream()
.map(Employee::getName)
.forEach(System.out::println);
}
}
package com.sunstone.stream;
import com.sunstone.lambda.Employee;
import org.junit.Test;
import java.util.*;
import java.util.stream.Stream;
public class TestStream {
public static Stream<Character> filterCharacter(String str) {
List<Character> list = new ArrayList<>();
for (Character ch : str.toCharArray()) {
list.add(ch);
}
return list.stream();
}
@Test
public static void test6() {
List<String> list = Arrays.asList("aaa", "bbb", "ccc");
Stream<Stream<Character>> stream1 = list.stream()
.map(TestStream::filterCharacter);
stream1.forEach((sm) -> {
sm.forEach(System.out::println);
});
Stream<Character> stream2 = list.stream()
.flatMap(TestStream::filterCharacter);
stream2.forEach(System.out::println);
}
}
3,排序
sorted() —— 产生一个新流,按自然排序(Comparable)
sorted(Comparator comp) —— 产生一个新流,按比较器定制排序(Comparator)
package com.sunstone.stream;
import com.sunstone.lambda.Employee;
import org.junit.Test;
import java.util.*;
import java.util.stream.Stream;
public class TestStream {
static List<Employee> emps = Arrays.asList(
new Employee("小孙", 17, 5555.55),
new Employee("小思", 27, 3333.33),
new Employee("小东", 37, 9999.99),
new Employee("小零", 50, 7777.77),
new Employee("小雪", 7, 8888.88),
new Employee("小豹", 60, 6666.66)
);
@Test
public void test7() {
List<String> list = Arrays.asList("aaa", "bbb", "ddd", "ccc");
list.stream().sorted().forEach(System.out::println);
emps.stream()
.sorted((e1, e2) -> {
if (e1.getAge() == e2.getAge()) {
return e1.getName().compareTo(e2.getName());
} else {
return e1.getAge() - e2.getAge();
}
})
.forEach(System.out::println);
}
}