Bootstrap

JAVA8 List的去重、过滤、映射(map)、分组、统计(sum、max、min、avg)、分页

目录

1.实现List对象集合的简单去重(distinct())

2.实现List集合的根据属性(name)去重

3.实现List对象集合的简单过滤(过滤为 null 的对象)

4.实现List对象集合中获取其中某一属性(weight)的List集合

5.实现List对象集合中根据对象(Apple)的某一属性(color)进行分组

6.实现List对象集合中求和、最大、最小、平均的统计(mapToDouble())

7.实现List对象集合的分页(skip()+limit())


1.实现List对象集合的简单去重(distinct())

核心代码:

list = list.stream().distinct().collect(Collectors.toList());

底层原理:

通过将 List类型 转换为 LinkedSet类型后,根据equals()方法和对象的hashCode()去重 的方式去重。

示例如下:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {
        List<Aoo> list = new ArrayList<Aoo>();
        Aoo a = new Aoo("a.name");
        Aoo b = a;
        Aoo c = new Aoo("c.name");
        list.add(a);
        list.add(b);
        list.add(c);
        System.out.println("list before operate : " + list);
        list = list.stream().distinct().collect(Collectors.toList());
        System.out.println("list after operate : " + list);
    }
  
}
class Aoo {
    private String name;

    public Aoo(String name) {
        this.name = name;
    }
    public Aoo() {}

    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Aoo [name=" + name + "]";
    }
}

执行结果:

list before operate : [Aoo [name=a.name], Aoo [name=a.name], Aoo [name=c.name]]
list after operate : [Aoo [name=a.name], Aoo [name=c.name]]

2.实现List集合的根据属性(name)去重

核心代码:(方法一)

list = list.stream().filter(o -> o.getName() != null).collect(
                Collectors.collectingAndThen(Collectors.toCollection(
                    () -> new TreeSet<>(Comparator.comparing(o -> o.getName()))), ArrayList<Aoo>::new));

示例如下:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {
        List<Aoo> list = Arrays.asList(new Aoo("a.name"), new Aoo("b.name"), new Aoo("c.name"));
        System.out.println("list before operate : " + list);
        list = list.stream().filter(o -> o.getName() != null).collect(
                Collectors.collectingAndThen(Collectors.toCollection(
                    () -> new TreeSet<>(Comparator.comparing(o -> o.getName()))), ArrayList<Aoo>::new));
        System.out.println("list after operate : " + list);
    }
}
class Aoo {
    private String name;

    public Aoo(String name) {
        this.name = name;
    }
    public Aoo() {}

    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Aoo [name=" + name + "]";
    }
}

执行结果:

list before operate : [Aoo [name=a.name], Aoo [name=a.name], Aoo [name=c.name]]
list after operate : [Aoo [name=a.name], Aoo [name=c.name]]

核心代码:(方法二)

public static void main(String[] args) {
    List<String> list = new ArrayList<>(0);
    list.add("1");
    list.add("222");
    list.add("333");
    Map<Integer, String> collect = list.stream().collect(Collectors.toMap(String::length, o -> o, (v1, v2) -> v1));
    list = new ArrayList<>(collect.values());
    System.out.println("result: " + list);
}

执行结果:

list before operate : [Aoo [name=a.name], Aoo [name=a.name], Aoo [name=c.name]]
list after operate : [Aoo [name=a.name], Aoo [name=c.name]]

3.实现List对象集合的简单过滤(过滤为 null 的对象)

核心代码:

list = list.stream().filter(aoo -> aoo != null).collect(Collectors.toList());

示例如下

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Test {

    public static void main(String[] args) {
        List<Aoo> list = Arrays.asList(new Aoo("a.name"), new Aoo("b.name"), new Aoo("c.name"), null);
        System.out.println("list before operate : " + list);
        list = list.stream().filter(aoo -> aoo != null).collect(Collectors.toList());
        System.out.println("list after operate : " + list);
    }
}

class Aoo {
    private String name;

    public Aoo(String name) {
        this.name = name;
    }
    public Aoo() {}

    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Aoo [name=" + name + "]";
    }
}

执行结果:

list before operate : [Aoo [name=a.name], Aoo [name=b.name], Aoo [name=c.name], null]
list after operate : [Aoo [name=a.name], Aoo [name=b.name], Aoo [name=c.name]]

4.实现List对象集合中获取其中某一属性(weight)的List集合

核心代码:

List<Double> collect = apples.stream().map(Apple::getWeight).collect(Collectors.toList());

示例如下:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Test {

    public static void main(String[] args) {
        List<Apple> apples = Arrays.asList(new Apple("yellow", 1.5), new Apple("red", 1.3), new Apple("green", 1.7));
        List<Double> collect = apples.stream().map(Apple::getWeight).collect(Collectors.toList());
        apples.forEach(System.out::println);
        collect.forEach(System.out::println);
    }
}
class Apple {
    private String color;
    private Double weight;

    public Apple(String color, Double weight) {
        this.color = color;
        this.weight = weight;
    }

    public Apple() {}

    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public Double getWeight() {
        return weight;
    }
    public void setWeight(Double weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Apple [color=" + color + ", weight=" + weight + "]";
    }
}

执行结果:

Apple [color=yellow, weight=1.5]
Apple [color=red, weight=1.3]
Apple [color=green, weight=1.7]
1.5
1.3
1.7

5.实现List对象集合中根据对象(Apple)的某一属性(color)进行分组

核心代码:

Map<String, List<Apple>> applesByColor = apples.stream().collect(Collectors.groupingBy(Apple :: getColor));

示例如下:

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Test {

    public static void main(String[] args) {
        List<Apple> apples = Arrays.asList(new Apple("yellow", 1.5), new Apple("red", 1.3), new Apple("red", 1.7));
        Map<String, List<Apple>> applesByColor = apples.stream().collect(Collectors.groupingBy(Apple::getColor));
        System.out.println("red Apples:");
        applesByColor.get("red").forEach(System.out::println);
        System.out.println("yellow Apples:");
        applesByColor.get("yellow").forEach(System.out::println);
    }
}
class Apple {
    private String color;
    private Double weight;

    public Apple(String color, Double weight) {
        this.color = color;
        this.weight = weight;
    }

    public Apple() {}

    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public Double getWeight() {
        return weight;
    }
    public void setWeight(Double weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Apple [color=" + color + ", weight=" + weight + "]";
    }
}

执行结果:

red Apples:
Apple [color=red, weight=1.3]
Apple [color=red, weight=1.7]
yellow Apples:
Apple [color=yellow, weight=1.5]

嵌套分组:

User user1 = new User("zhangsan", "beijing", 10);
User user2 = new User("zhangsan", "beijing", 20);
User user3 = new User("lisi", "shanghai", 30);
List<User> list = new ArrayList<User>();
list.add(user1);
list.add(user2);
list.add(user3);
Map<String, Map<String, List<User>>> collect
        = list.stream().collect(
                Collectors.groupingBy(
                        User::getAddress, Collectors.groupingBy(User::getName)
                )
);
System.out.println(collect);

分组计数:

Map<String, Long> collect = list.parallelStream().collect(Collectors.groupingBy(User::getAddress,Collectors.counting()));

根据某一属性,对另一属性进行分组:

Map<String, List<Long>> collect = list.parallelStream().collect(Collectors.groupingBy(User::getAddress, Collectors.mapping(User::getWeight, Collectors.toList())));

6.实现List对象集合中求和、最大、最小、平均的统计(mapToDouble())

除了统计double类型,还有int(mapToInt)和long(mapToLong)

核心代码:

double sum = apples.stream().mapToDouble(Apple::getWeight).sum(); //和
OptionalDouble max = apples.stream().mapToDouble(Apple::getWeight).max(); //最大
OptionalDouble min = apples.stream().mapToDouble(Apple::getWeight).min(); //最小
OptionalDouble average = apples.stream().mapToDouble(Apple::getWeight).average(); //平均值

示例如下:

import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;

public class Test {

    public static void main(String[] args) {
        List<Apple> apples = Arrays.asList(new Apple("yellow", 1.5), new Apple("red", 1.3), new Apple("green", 1.7));
        double sum = apples.stream().mapToDouble(Apple::getWeight).sum(); //和
        OptionalDouble max = apples.stream().mapToDouble(Apple::getWeight).max(); //最大
        OptionalDouble min = apples.stream().mapToDouble(Apple::getWeight).min(); //最小
        OptionalDouble average = apples.stream().mapToDouble(Apple::getWeight).average(); //平均值
        System.out.println("sum:" + sum); //和
        System.out.println("max:" + max); //最大
        System.out.println("min:" + min); //最小
        System.out.println("average:" + average); //平均值
    }
}
class Apple {
    private String color;
    private Double weight;

    public Apple(String color, Double weight) {
        this.color = color;
        this.weight = weight;
    }
    public Apple() {}

    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public Double getWeight() {
        return weight;
    }
    public void setWeight(Double weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Apple [color=" + color + ", weight=" + weight + "]";
    }
}

执行结果:

sum:4.5
max:OptionalDouble[1.7]
min:OptionalDouble[1.3]
average:OptionalDouble[1.5]

7.实现List对象集合的分页(skip()+limit())

核心代码:

List<User> resultList = list.stream().skip(pageSize * (pageNum - 1)).limit(pageSize).collect(Collectors.toList());

示例如下:

import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;

public class Test {

    public static void main(String[] args) {
        List<Apple> apples = Arrays.asList(new Apple("yellow", 1.5), new Apple("red", 1.3), new Apple("green", 1.7));
        int pageNum = 2;
        int pageSize = 1;
        List<Apple> resultList = apples.stream().skip(pageSize * (pageNum - 1)).limit(pageSize).collect(Collectors.toList());
        System.out.println("resultList:" + resultList); // 第2页,每页1条数据
    }
}
class Apple {
    private String color;
    private Double weight;

    public Apple(String color, Double weight) {
        this.color = color;
        this.weight = weight;
    }
    public Apple() {}

    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public Double getWeight() {
        return weight;
    }
    public void setWeight(Double weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Apple [color=" + color + ", weight=" + weight + "]";
    }
}

执行结果:

resultList:[Apple [color=red, weight=1.3]]

;