数据流工作的方式:
List<String> myList = Arrays.asList("a1","a2","b1","c1","c2");
myList.stream().filter(s -> s.startsWith("c"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
数据流可以从多种数据源创建,尤其是集合。List和Set支持新方法stream()和parallelStream().来创建串行流和并行流。
判断list集合是否存在第一个元素
myList.stream().findFirst().isPresent();
解释: 查找list的第一元素,判断该list的第一个元素是否存在。 返回值为 boolean
改变list的值并计算出其平均值
System.out.println(Arrays.stream(new int[]{1,2,3,4})
.map(n -> 2 * n + 1)
.average().getAsDouble());
针对于java8中的数据流不能被复用。一旦调用了任何终止操作,数据流就关闭了:
Stream<String> stream = Stream.of("d2","a2","b1","b3","c")
.filter(s -> s.startsWith("a"));
stream.anyMatch(s -> true); //ok
stream.noneMatch(s -> true); //exception
为了克服这个限制,在执行终止操作创建新的数据流调用链。创建一个数据流供应器,来构建新的数据流,并且设置好所有衔接操作:
Supplier<Stream<String>> streamSupplier =
() -> Stream.of("d2","a2","b1","b3","c")
.filter(s -> s.startsWith("a"));
streamSupplier.get().anyMatch(s -> true); // ok
streamSupplier.get().noneMatch(s -> true); // ok
java8的groupingBy的使用:
List<Person> personList = Arrays.asList(
new Person("Max", 18),
new Person("Peter", 23),
new Person("Pamela", 23),
new Person("David", 12)
);
Map<Integer,List<Person>> listMap = personList.stream()
.collect(Collectors.groupingBy(p -> p.age));
listMap.forEach((age,p) -> System.out.format("age %s: %s\n", age,p));
java8的统计实现
List<Person> personList = Arrays.asList(
new Person("Max", 18),
new Person("Peter", 23),
new Person("Pamela", 23),
new Person("David", 12)
);
IntSummaryStatistics summaryStatistics =
personList.stream()
.collect(Collectors.summarizingInt(p -> p.getAge()));
System.out.println(summaryStatistics);
//IntSummaryStatistics{count=4, sum=76, min=12, average=19.000000, max=23}
将所有人连接为一个字符串
String phrase = personList.stream()
.filter(p -> p.getAge() >= 18)
.map(p -> p.getName())
.collect(Collectors.joining(" and "," In Germany ", " are of legal age."));
System.out.println(phrase);
// joining一共有三个参数
// 第一个是: 分隔符 (必填)
//第二个是: 前缀(可选)
//第三个是: 后缀(可选)
数据初始化:
public static List<Person> dealPerson() {
List<Person> personList = Arrays.asList(new Person("Max", 17),
new Person("Tony", 18), new Person("Tom", 19),
new Person("Amy", 20), new Person("David", 21),
new Person("Peter", 22), new Person("Pamela", 23));
return personList;
}
具体实现值
public static void main(String[] args) {
List<Person> personList = DealPersonUtil.dealPerson();
// java8的groupingBy使用
log.info(personList.stream()
.collect(Collectors.groupingBy(Person::getAge)).toString());
// 判断list集合是否存在第一个元素
System.out.println(personList.stream().findFirst().isPresent());
/**
将所有人连接为一个字符串 有3个参数(可选)
第一个是: 分隔符
第二个是: 前缀
第三个是: 后缀
**/
String phrase = personList.stream().filter(p -> p.getAge() >= 18)
.map(Person::getName)
.collect(Collectors.joining(" and ", "In Germany ", "are of legal age."));
System.out.println(phrase);
//查看年龄是20的人数
long count = personList.stream()
.filter(person -> person.getAge() == 20)
.count();
System.out.println(count);
long system = personList.stream()
.filter(person -> {
System.out.println(person.getName());
return person.getAge() == 20;
}).count();
System.out.println(system);
//使用stream查找年龄最小的用户
Person person = personList.stream()
.min(Comparator.comparing(item -> item.getAge())).get();
//使用stream查找年龄最大的用户
Person personMax = personList.stream()
.max(Comparator.comparing(item -> item.getAge())).get();
System.out.println(personMax.toString());
//reduce操作实现累加
int countNumber = Stream.of(1,2,3,4,5,6,7,8,9)
.reduce(0,(acc,element) -> acc + element);
System.out.println(countNumber);
}