首页 > 其他分享 >filter()方法数据过滤

filter()方法数据过滤

时间:2022-12-01 08:56:17浏览次数:41  
标签:Streamprocess String stream list filter 过滤 方法 public name

 数据过滤类似于SQL中的WHERE语句,是在杂乱的数据中筛选出需要的数据。下面就介绍一下filter()方法。

filter()是Stream接口提供的一个过滤方法。该方法以lambda表达式的逻辑过滤流中的元素。若要展示数据需可以使用 Stream提供的collect()方法进行封装。

基本使用方法:

下面是一个简单的例子,展示了如和利用filter()过滤出3的倍数:

 

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Filtertest {
    public static void main(String[] args) {
        List<Integer>list  = new ArrayList<>();
        for(int i = 1;i<=30;i ++){
            list.add(i);                                //向集合中添加数据
        }
        printeach("过滤前" ,list);
        Stream<Integer>stream = list.stream();         //获取集合流对象
        stream= stream.filter(n-> n % 3 == 0);         //过流出流中3的倍数(关键代码)
        List<Integer>res = stream.collect(Collectors.toList());//将流重新封装成List集合
 
        printeach("过滤后",res);
 
    }
    static void printeach(String S,List list)
    {   System.out.print(S);
        list.stream().forEach(n->{
            System.out.print(n+" ");
        });  //不要忘记分号
        System.out.println(" ");
    }
 
}


运行结果如下: 

过滤前1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30  
过滤后3 6 9 12 15 18 21 24 27 30  


2 .结合流处理用

filter()方法通常会和流处理一起用。而且过滤的不仅仅是数字。下面这个例子首先添加了一个员工集合。并记录了姓名,年龄,薪资,性别,部门。最后通过filter()方法找出了女性的员工。

代码如下:

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Stream;
import java.util.stream.Collectors;
public class Streamprocess{
    private String name;
    private int age;
    private double salary;
    private String sex;
    private String dept;
    public Streamprocess(String name,int age,double salary,String sex, String dept)
    {
        this.name = name;
        this.age =age;
        this.salary = salary;
        this.sex = sex;
        this.dept = dept;
    }
    public String toString() {
        return "name="+name+"age="+age+"salary="+salary+"sex="+sex+"dept="+dept;
    }
    public String getName() { return name;}
    public int getAge() {return age;}
    public double getSalary() {return salary;}
    public String getSex() {return sex;}
    public String getDept() {return dept;}
    static List<Streamprocess>getEm(){
    List<Streamprocess>list = new ArrayList<Streamprocess>();
    list.add(new Streamprocess("小一",50,4000,"男","一部"));
    list.add(new Streamprocess("小二",10,4000,"女","一部"));
    list.add(new Streamprocess("小三",30,5000,"男","二部"));
    list.add(new Streamprocess("小四",40,5000,"男","二部"));
    list.add(new Streamprocess("小五",60,3000,"女","三部"));
    list.add(new Streamprocess("小六",10,4000,"男","三部"));
    return list;
    }
    public static void main(String[] args) {
        List<Streamprocess>list1 = Streamprocess.getEm();
        Stream<Streamprocess>stream = list1.stream();
        stream = stream.filter(people->"女".equals(people.getSex()));
        List<Streamprocess>result = stream.collect(Collectors.toList());
        for(Streamprocess emp:result) {
            System.out.println(emp);
 
        }
    }
}


运行结果:

name=小二age=10salary=4000.0sex=女dept=一部
name=小五age=60salary=3000.0sex=女dept=三部


希望对你有帮助~~~

标签:Streamprocess,String,stream,list,filter,过滤,方法,public,name
From: https://www.cnblogs.com/demc/p/16940370.html

相关文章

  • 如何使用 CSS filter 把整个网页快速切换成灰色主题 All In One
    如何使用CSSfilter把整个网页快速切换成灰色主题AllInOne为追思疫情中逝世的同袍们,在哀悼日这一天,好像整个世界都是灰色的。感觉最深应该就是这一天你打开任何主......
  • Pyinstaller 打包图片方法
    前戏​​Pyinstaller​​ 可以用来将python程序打包成独立可执行程序,让python程序能在没有装python环境的机器上运行。有时候用python写了一个小程序,但是共享给别人时,......
  • 使用Excel数据透视表将一维表转二维表的方法
    问题:从平台上下载的专区课程学习清单,竟然是一维表,也就是姓名、课程、进度,这个表存在2个问题,一是无法快速的知道某人现在学完了几门课程,只能数它的行数,我想要的目的是将这个......
  • Python threading Thread多线程的使用方法
    PythonthreadingThread多线程的使用方法目录​​PythonthreadingThread多线程的使用方法​​​​1.使用Threading模块创建线程​​​​2.Thread线程同步​​​​3.使用......
  • IDEA如何打开structure窗口(类视图方法)
    Structure结构视图默认是不打开的在菜单View>ToolWindows>Structure或者使用Alt+7打开类视图显示:......
  • 迁移 vscode 配置的方法
    来源:https://stackoverflow.com/questions/35368889/how-can-i-export-settingsWiththecurrentversionofVisualStudioCodeasofthiswriting(1.22.1),youcan......
  • 对象,数组及字符串的操作方法
    一、对象操作方法  对象的操作的语法分为点语法和数组关联语法两种,点语法是对象名.键,数组关联语法是对象名['键']。值得注意的是,点语法的键不能是变量,变量必须要用数......
  • DRF解析器使用方法和配置案例
    DjangoREST中的解析器用于解析传入HTTP请求的内容。在HTTP请求中以字符串格式接收数据。解析器将根据HTTP请求中收到的Content-Type标头将HTTP内容解析为pyth......
  • Dubbo RPC基于Filter的自定义参数校验
    在web应用中,我们经常使用注解的方式来校验参数,使得业务开发不用过分关注参数校验的逻辑但是在现有的微服务架构中,常常只是作为一个服务提供rpc服务的方式,那是不是还是退......
  • 调用运行时类的指定结构:属性、方法、构造器
    packageday3;importorg.junit.Test;importjava.lang.reflect.Field;importjava.lang.reflect.Method;//调用运行时类的指定结构:属性、方法、构造器publicclassR......