jdk8,stream流collect方法:处理集合方法
前提
需要了解@FunctionalInterface:函数式接口,很多方法参数都是函数式接口,需要了解的可以看下这篇文章
通俗易懂,jdk8-@FunctionalInterface:函数式接口
数据转换
1:转List
private List<Integer> list = Arrays.asList(5, 2, 4, 1, 6, 3, 10, 8, 7, 9);
@Test
public void toList() {
List<String> collect = list.stream().map(t -> t + "*").collect(Collectors.toList());
System.out.println("collect = " + collect);
}
输出:对集合每个对象拼接"*",然后将拼接结果转为List
collect = [5*, 2*, 4*, 1*, 6*, 3*, 10*, 8*, 7*, 9*]
2:转Set
private List<Integer> list = Arrays.asList(5, 2, 4, 1, 6, 3, 10, 8, 7, 9);
@Test
public void toSet() {
Set<Integer> collect = list.stream().collect(Collectors.toSet());
System.out.println("collect = " + collect);
}
输出
collect = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3:转Collection
private List<Integer> list = Arrays.asList(5, 2, 4, 1, 6, 3, 10, 8, 7, 9);
@Test
public void toCollection() {
Set<Integer> collect = list.stream().collect(Collectors.toCollection(TreeSet::new));
System.out.println("collect.getClass().getName() = " + collect.getClass().getName());
}
输出:将List转成了TreeSet
collect.getClass().getName() = java.util.TreeSet
4:转Map
1.基础转化
private List<Integer> list = Arrays.asList(5, 2, 4, 1, 6, 3, 10, 8, 7, 9);
@Test
public void toMap() {
Map<Integer, Integer> collect = list.stream().collect(Collectors.toMap(Function.identity(), v -> v * 10));
System.out.println("collect = " + collect);
}
这里使用到了Function.identity(),其实对应的就是我们平时的写法:t -> t,意思就是传入对象t,然后把这个传入的对象直接返回
/**
* Returns a function that always returns its input argument.
*
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
*/
static <T> Function<T, T> identity() {
return t -> t;
}
输出:key为对象自身,value为对象自身 * 10
collect = {1=10, 2=20, 3=30, 4=40, 5=50, 6=60, 7=70, 8=80, 9=90, 10=100}
2.处理key值重复(如果key值重复不使用这个mergeFunction对象,代码会报错!!!)
private List<Integer> list = Arrays.asList(5, 2, 4, 1, 6, 3, 10, 8, 7, 9, 9);
@Test
public void toMapWithMergeFunction() {
Map<Integer, Integer> collect = list.stream().collect(Collectors.toMap(Function.identity(), v -> v * 10, (first, second) -> second));
System.out.println("collect = " + collect);
}
这里使用到了mergeFunction,也就是:(first, second) -> second,意思就是key值重复的话,使用重复的这个做为key
public static <T, K, U>
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction) {
return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
}
输出:key为对象自身,value为对象自身 * 10
collect = {1=10, 2=20, 3=30, 4=40, 5=50, 6=60, 7=70, 8=80, 9=90, 10=100}
3.指定Map类型
private List<Integer> list = Arrays.asList(5, 2, 4, 1, 6, 3, 10, 8, 7, 9);
@Test
public void toMapWithMapSupplier() {
Map<Integer, Integer> collect = list.stream().collect(Collectors.toMap(Function.identity(), v -> v * 10, (first, second) -> second, LinkedHashMap::new));
System.out.println("collect.getClass().getName() = " + collect.getClass().getName());
}
这里使用到了mapSupplier,也就是:LinkedHashMap::new,意思就是使用LinkedHashMap类型的Map
public static <T, K, U>
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction) {
return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
}
输出:
collect.getClass().getName() = java.util.LinkedHashMap