集合分组:Collectors.groupingBy()

数据库中有groupby 分组,Java里面其实也有的,感觉很好用。对List里面的数据进行分组,一行代码解决问题,太爽了

话不多说,上代码。

people实体:按照人员的地址分组,使用map<key,List<people>>接收。

public class People {
    private Integer id;
    private String name;
    private String address;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public People() {
    }

    public People(Integer id, String name, String address, Integer age) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.age = age;
    }

    @Override
    public String toString() {
        return "People{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", age=" + age +
                '}';
    }
}

实例方法:


public class Test {
    public static void main(String[] args) {
        People people1=new People(1,"only-qi1","11",11);
        People people2=new People(2,"only-qi2","12",12);
        People people3=new People(3,"only-qi3","14",13);
        People people4=new People(1,"only-qi4","14",14);
        People people5=new People(2,"only-qi5","13",15);
        People people6=new People(1,"only-qi6","11",16);
        ArrayList<People>arrayList=new ArrayList<>();
        arrayList.add(people1);
        arrayList.add(people2);
        arrayList.add(people3);
        arrayList.add(people4);
        arrayList.add(people5);
        arrayList.add(people6);
        Map<String,List<People>>map=arrayList.stream().collect(Collectors.groupingBy(People::getAddress));
        for (String s : map.keySet()) {
            System.out.println("key值是:"+s+"========="+"value的值是"+map.get(s));
        }
    }
}

执行结果:

按照多个字段分组:

 Map<String, List<SchoolYearWarnStatisticVO>> map = schoolYearWarnStatisticVOS.stream().collect(Collectors.groupingBy(p -> p.getSchoolName()+"_"+p.getGradeType()));

===另外看到两个好用的方法,分享给大家。开发中肯定用的到:====

1.String.join()方法的方法和使用:将数组或集合以某拼接符拼接到一起形成新的字符串

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Mxy");
        list.add("StringUtils");
        list.add("join");
        String join = String.join("-",list);//传入String类型的List集合,使用"-"号拼接
        System.out.println("=========only-qi========"+join);
        String[] s = new String[]{"Yuan","Mxy"};//传入String类型的数组,使用"-"号拼接
        String join2 = String.join("-",s);
        System.out.println("=========only-qi========"+join2);
    }

执行的结果:

2.StringUtils.join()方法的方法和使用:将数组或集合以某拼接符拼接到一起形成新的字符串。 

首先引入common-lang3依赖

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>

代码:

package com.gq.upup01.day01;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class Up02 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Mxy");
        list.add("StringUtils");
        list.add("join");
        String join = StringUtils.join(list,"-");//传入String类型的List集合,使用"-"号拼接
        System.out.println("********************common-lang3"+join);

        String[] s = new String[]{"Yuan","Mxy"};//传入String类型的数组,使用"-"号拼接
        String join2 = StringUtils.join(s,"-");
        System.out.println("********************common-lang3"+join2);
    }
}

执行结果:

=======java 8 根据对象的某个字段排序(升序,降序)=====

//根据Dict对象的sort字段降序排序
dictList.sort(Comparator.comparing(Dict::getSort).reversed());
//根据Dict对象的sort字段升序排序
dictList.sort(Comparator.comparing(Dict::getSort));
=====================Collectors.toMap()==============================
 public static void main(String[] args) {
        List<Teacher> userList = Lists.newArrayList(
                new Teacher().setId(1).setName("张三"),
                new Teacher().setId(2).setName("李四"),
                new Teacher().setId(3).setName("王五")
        );
        Map<Integer, String> map = userList.stream().collect(Collectors.toMap(Teacher::getId, Teacher::getName));
        System.out.println("**********************1"+map);
        Map<Integer, Teacher> map1 = userList.stream().collect(Collectors.toMap(Teacher::getId, t -> t));
        System.out.println("**********************2"+map1);


    }
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true) // 链式方法
public class Teacher {
    private Integer id;
    private String name;
}

运行结果:

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

only-qi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值