编程语言
首页 > 编程语言> > java使用Stream简单操作集合

java使用Stream简单操作集合

作者:互联网

 

效果图

 

 本项目使用springboot

pom依赖

<!--lombok依赖-->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.16</version>
		</dependency>

 <!--引入junit单元测试依赖-->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>

  如果还有问题引入

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

  springboot版本

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
<!--		<version>2.3.4.RELEASE</version>-->
		<relativePath/>
	</parent>

  

package com.java.list;

import lombok.Builder;
import lombok.Data;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/8/30 12:36
 */
@Data
@Builder
public class ClassRoom {

    /**
     * 学生姓名
     */
    private String name;

    /**
     * 学生年龄
     */
    private Integer age;

    /**
     * 学生爱好
     */
    private String hobby;

    /**
     * 学生的特长
     */
    private String specialSkill;

    /**
     * 对于爱好的喜欢程度,01特别喜欢,02一般喜欢,03不喜欢
     */
    private String status;
}

  

package com.java.list;

import org.junit.Test;

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

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/8/30 12:35
 */
public class CollectTest {


    @Test
    public void testList(){

        List<ClassRoom> classRoomList=new ArrayList<>();
        classRoomList.add(ClassRoom.builder().name("小红").age(10).hobby("羽毛球").specialSkill(null).status("01").build());
        classRoomList.add(ClassRoom.builder().name("小六子").age(12).hobby("羽毛球,足球").specialSkill(null).status("01").build());
        classRoomList.add(ClassRoom.builder().name("小白").age(10).hobby("篮球").specialSkill("钢琴").status("03").build());
        classRoomList.add(ClassRoom.builder().name("小绿").age(12).hobby("羽毛球").specialSkill(null).status("01").build());
        classRoomList.add(ClassRoom.builder().name("小黑").age(12).hobby("乒乓球").specialSkill(null).status("02").build());
        classRoomList.add(ClassRoom.builder().name("小红帽").age(16).hobby("跳绳").specialSkill(null).status("02").build());
        classRoomList.add(ClassRoom.builder().name("小得子").age(12).hobby("羽毛球,篮球").specialSkill(null).build());

        //选出对爱好一般喜欢的学生
        List<ClassRoom> collect = classRoomList.stream().filter(a -> a.toString().contains("02")).collect(Collectors.toList());
        System.out.println("选出对爱好一般喜欢的学生");
        collect.forEach(a->{
            System.out.println(a);
        });

        //选出年龄大于12岁的
        List<ClassRoom> collect2 = classRoomList.stream().filter(k -> k.getAge() > 12).collect(Collectors.toList());
        System.out.println("选出年龄大于12岁的");
        collect2.forEach(k->{
            System.out.println(k);
        });

        //选出特长不为空的学生
        List<ClassRoom> collect1 = classRoomList.stream().filter(b -> b.getSpecialSkill() != null).collect(Collectors.toList());
        System.out.println("选出特长不为空的学生");
        collect1.forEach(b->{
            System.out.println(b);
        });

        //按照年龄排序
        List<ClassRoom> collect3 = classRoomList.stream().sorted(Comparator.comparing(ClassRoom::getAge)).collect(Collectors.toList());
        System.out.println("按照年龄排序");
        collect3.forEach(c->{
            System.out.println(c);
        });


    }
}

  

 

标签:ClassRoom,12,java,name,Stream,import,集合,hobby,classRoomList
来源: https://www.cnblogs.com/q202105271618/p/16638990.html