Java教程

java8快速筛选list

本文主要是介绍java8快速筛选list,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
public class StreamTest {
 
    public static void main(String[] args) {
        List<Student> allList = new ArrayList<Student>();
 
        Student st1 = new Student("小王",26,1,"计算机");
        allList.add(st1);
        Student st2 = new Student("小张",21,1,"电气");
        allList.add(st2);
        Student st3 = new Student("小红",22,1,"人文");
        allList.add(st3);
        Student st4 = new Student("小李",23,1,"计算机");
        allList.add(st4);
 
        System.out.println("**************************目的:只取专业为计算机行业的集合*************************");
//        allList = allList.stream().filter(Student -> Student.getProfessional().equals("计算机")).collect(Collectors.toList());
//        allList.forEach(Student -> {
//            System.out.println(Student.getName());
//        });
        System.out.println("**************************目的:取专业为计算机和人文专业的集合*************************");
        List<String> str=new ArrayList<>();
        str.add("计算机");
        str.add("人文");
        allList = allList.stream().filter(Student -> str.contains(Student.getProfessional())).collect(Collectors.toList());
        allList.forEach(Student -> {
            System.out.println(Student.getName());
        });
 
    }

这篇关于java8快速筛选list的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!