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

时间:2025-03-20 10:46:38

目录

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 = ().distinct().collect(());

底层原理:

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

示例如下:

import ;
import ;
import ;

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

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

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

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

执行结果:

list before operate : [Aoo [name=], Aoo [name=], Aoo [name=]]
list after operate : [Aoo [name=], Aoo [name=]]

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

核心代码:(方法一)

list = ().filter(o -> () != null).collect(
                ((
                    () -> new TreeSet<>((o -> ()))), ArrayList<Aoo>::new));

示例如下:

import ;
import ;
import ;
import ;
import ;
import ;

public class Test {
    public static void main(String[] args) {
        List<Aoo> list = (new Aoo(""), new Aoo(""), new Aoo(""));
        ("list before operate : " + list);
        list = ().filter(o -> () != null).collect(
                ((
                    () -> new TreeSet<>((o -> ()))), ArrayList<Aoo>::new));
        ("list after operate : " + list);
    }
}
class Aoo {
    private String name;

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

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

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

执行结果:

list before operate : [Aoo [name=], Aoo [name=], Aoo [name=]]
list after operate : [Aoo [name=], Aoo [name=]]

核心代码:(方法二)

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

执行结果:

list before operate : [Aoo [name=], Aoo [name=], Aoo [name=]]
list after operate : [Aoo [name=], Aoo [name=]]

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

核心代码:

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

示例如下

import ;
import ;
import ;

public class Test {

    public static void main(String[] args) {
        List<Aoo> list = (new Aoo(""), new Aoo(""), new Aoo(""), null);
        ("list before operate : " + list);
        list = ().filter(aoo -> aoo != null).collect(());
        ("list after operate : " + list);
    }
}

class Aoo {
    private String name;

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

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

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

执行结果:

list before operate : [Aoo [name=], Aoo [name=], Aoo [name=], null]
list after operate : [Aoo [name=], Aoo [name=], Aoo [name=]]

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

核心代码:

List<Double> collect = ().map(Apple::getWeight).collect(());

示例如下:

import ;
import ;
import ;

public class Test {

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

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

    public Apple() {}

    public String getColor() {
        return color;
    }
    public void setColor(String color) {
         = color;
    }
    public Double getWeight() {
        return weight;
    }
    public void setWeight(Double 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 = ().collect((Apple :: getColor));

示例如下:

import ;
import ;
import ;
import ;

public class Test {

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

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

    public Apple() {}

    public String getColor() {
        return color;
    }
    public void setColor(String color) {
         = color;
    }
    public Double getWeight() {
        return weight;
    }
    public void setWeight(Double 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>();
(user1);
(user2);
(user3);
Map<String, Map<String, List<User>>> collect
        = ().collect(
                (
                        User::getAddress, (User::getName)
                )
);
(collect);

分组计数:

Map<String, Long> collect = ().collect((User::getAddress,()));

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

Map<String, List<Long>> collect = ().collect((User::getAddress, (User::getWeight, ())));

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

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

核心代码:

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

示例如下:

import ;
import ;
import ;

public class Test {

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

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

    public String getColor() {
        return color;
    }
    public void setColor(String color) {
         = color;
    }
    public Double getWeight() {
        return weight;
    }
    public void setWeight(Double 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 = ().skip(pageSize * (pageNum - 1)).limit(pageSize).collect(());

示例如下:

import ;
import ;
import ;

public class Test {

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

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

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

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

执行结果:

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