// org.springframework.boot.logging.LoggingSystem
/**
* Detect and return the logging system in use. Supports Logback and Java Logging.
* @param classLoader the classloader
* @return the logging system
*/
public static LoggingSystem get(ClassLoader classLoader) {
String loggingSystem = System.getProperty(SYSTEM_PROPERTY);
if (StringUtils.hasLength(loggingSystem)) {
if (NONE.equals(loggingSystem)) {
return new NoOpLoggingSystem();
}
return get(classLoader, loggingSystem);
}
return SYSTEMS.entrySet()
// 获取流对象
.stream()
// 过滤
.filter((entry) -> ClassUtils.isPresent(entry.getKey(), classLoader))
// 映射
.map((entry) -> get(classLoader, entry.getValue()))
// 返回第一个值
.findFirst()
// 不存在则抛出异常
.orElseThrow(() -> new IllegalStateException("No suitable logging system located"));
}
// org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
MutablePropertySources propertySources = environment.getPropertySources();
propertySources.stream()
// 映射
.map(JsonPropertyValue::get)
// 过滤掉空值
.filter(Objects::nonNull)
// 查找第一个(预期结果最多为一个的情况)
.findFirst()
// 如果存在值 则进行处理 不存在 则不产生影响
.ifPresent((v) -> processJson(environment, v));
}
// org.springframework.boot.convert.CollectionToDelimitedStringConverter
private Object convert(Collection<?> source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source.isEmpty()) {
return "";
}
return source.stream()
// 进行映射
.map((element) -> convertElement(element, sourceType, targetType))
// 结果收集
.collect(Collectors.joining(getDelimiter(sourceType)));
}
private boolean consumesRequestBody(Method method) {
return Stream.of(method.getParameters())
.anyMatch((parameter) -> parameter.getAnnotation(Selector.class) == null);
}
BigDecimal sum = childList.stream()
// 映射类型
.map(TtrdTestChild::getVolume)
// 初始值为0 进行汇总
.reduce(BigDecimal.ZERO,BigDecimal::add);
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.insightfullogic.java8.examples.chapter1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
/**
* Domain class for a popular music artist.
*
* @author Richard Warburton
*/
public final class Artist {
private String name;
private List<Artist> members;
private String nationality;
public Artist(String name, String nationality) {
this(name, Collections.emptyList(), nationality);
}
public Artist(String name, List<Artist> members, String nationality) {
Objects.requireNonNull(name);
Objects.requireNonNull(members);
Objects.requireNonNull(nationality);
this.name = name;
this.members = new ArrayList<>(members);
this.nationality = nationality;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the members
*/
public Stream<Artist> getMembers() {
return members.stream();
}
/**
* @return the nationality
*/
public String getNationality() {
return nationality;
}
public boolean isSolo() {
return members.isEmpty();
}
public boolean isFrom(String nationality) {
return this.nationality.equals(nationality);
}
@Override
public String toString() {
return getName();
}
public Artist copy() {
List<Artist> members = getMembers().map(Artist::copy).collect(toList());
return new Artist(name, members, nationality);
}
}
编写一个 函数,接收艺术家列表作为参数,返回一个字符串列表,其中包含艺术家的姓名和国籍。
public static List<String> getNamesAndOrigins(List<Artist> artists) {
return artists.stream()
.flatMap(artist -> Stream.of(artist.getName(), artist.getNationality()))
.collect(toList());
}
编写一个函数,接受专辑列表作为参数,返回一个由最多包含3首歌曲的专辑组成的列表。
public static List<Album> getAlbumsWithAtMostThreeTracks(List<Album> input) {
return input.stream()
.filter(album -> album.getTrackList().size() <= 3)
.collect(toList());
}
获取所有艺术家的数量
public static int countBandMembersInternal(List<Artist> artists) {
// NB: readers haven't learnt about primitives yet, so can't use the sum() method
return artists.stream()
.map(artist -> artist.getMembers().count())
.reduce(0L, Long::sum)
.intValue();
//return (int) artists.stream().flatMap(artist -> artist.getMembers()).count();
}
找出名字最长的艺术家,分别使用收集器和reduce高阶函数
import com.insightfullogic.java8.examples.chapter1.Artist;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Comparator.comparing;
public class LongestName {
private static Comparator<Artist> byNameLength = comparing(artist -> artist.getName().length());
public static Artist byReduce(List<Artist> artists) {
return artists.stream()
.reduce((acc, artist) -> {
return (byNameLength.compare(acc, artist) >= 0) ? acc : artist;
})
.orElseThrow(RuntimeException::new);
}
public static Artist byCollecting(List<Artist> artists) {
return artists.stream()
.collect(Collectors.maxBy(byNameLength))
.orElseThrow(RuntimeException::new);
}
}