rxjava : 过滤操作符:filter(条件过滤)、 distinct(去重)、ofType(类型过滤)、buffer(缓存)

本文主要是介绍rxjava : 过滤操作符:filter(条件过滤)、 distinct(去重)、ofType(类型过滤)、buffer(缓存),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

filter :

根据过滤规则过滤数据

@Test
public void filter() {ArrayList<Student> students = new ArrayList<>();students.add(new Student("1", 1));students.add(new Student("2", 20));students.add(new Student("2", 21));students.add(new Student("2", 23));students.add(new Student("3", 3));students.add(new Student("4", 4));students.add(new Student("5", 50));students.add(new Student("6", 6));students.add(new Student("7", 7));Disposable disposable = Observable.fromIterable(students).filter(student -> student.getAge() > 21).subscribe(student ->System.out.println("student==========="+ student.toString()));
}
//student===========Student{name='2', age=23}
//student===========Student{name='5', age=50}

distinct : 去重

/*** 去重 : distinct*/
@Test
public void distinct1() {Disposable disposable = Observable.just("a", "d", "b", "c", "a", "e", "b", "c", "a", "b").distinct().subscribe(new Consumer<String>() {@Overridepublic void accept(String s) {System.out.println("s=============" + s);}});
}
//s=============a
//s=============d
//s=============b
//s=============c
//s=============e/*** 去重对象 : distinct*/
@Test
public void distinct2() {ArrayList<Student> students = new ArrayList<>();students.add(new Student("1", 1));students.add(new Student("2", 20));students.add(new Student("2", 21));students.add(new Student("2", 23));students.add(new Student("3", 3));students.add(new Student("4", 4));students.add(new Student("5", 50));students.add(new Student("6", 6));students.add(new Student("7", 7));Disposable disposable = Observable.fromIterable(students).distinct(new Function<Student, String>() {@Overridepublic String apply(Student student) throws Exception {return student.getName();//如果两个学生name一样就过滤(去重)//return student.getName() + student.getAge(); //如果多个条件同时,可以采取属性拼接的方式}}).subscribe(new Consumer<Student>() {@Overridepublic void accept(Student student) throws Exception {System.out.println("student==========="+ student.toString());}});
}
//student===========Student{name='1', age=1}
//student===========Student{name='2', age=20}
//student===========Student{name='3', age=3}
//student===========Student{name='4', age=4}
//student===========Student{name='5', age=50}
//student===========Student{name='6', age=6}
//student===========Student{name='7', age=7}
public class Student {private String name;private int age;public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}

distinct : 去重二

/*** 去重对象 2个条件并再次组合成集合 : distinct*/public void distinct3() {ArrayList<Student> students = getStudents();Disposable disposable = Observable.fromIterable(students).distinct(new Function<Student, Integer>() {@Overridepublic Integer apply(Student student) throws Exception {//返回过滤条件,如果为默认值0,不过滤return student.getAge() != 0 ?student.getAge() :new Random().nextInt(Integer.MAX_VALUE);}}).distinct(new Function<Student, String>() {@Overridepublic String apply(Student student) throws Exception {//返回过滤条件,如果为空,不过滤return !TextUtils.isEmpty(student.getName()) ?student.getName() :UUID.randomUUID().toString();}}).toList()   //再合并各个数据.subscribe(new Consumer<List<Student>>() {@Overridepublic void accept(List<Student> students) throws Exception {if (students != null) {for (int i = 0; i < students.size(); i++) {System.out.println("students======"+ students.get(i).toString());}}}}, new Consumer<Throwable>() {@Overridepublic void accept(Throwable throwable) throws Exception {System.out.println("throwable======"+ throwable.getMessage());}});}//students======Student{name='1', age=11, school='11'}//students======Student{name='2', age=21, school='21'}//students======Student{name='3', age=31, school='31'}//students======Student{name='4', age=41, school='41'}//students======Student{name='5', age=51, school='51'}//students======Student{name='6', age=61, school='61'}//students======Student{name='7', age=71, school='71'}//students======Student{name='8', age=81, school='81'}//students======Student{name='9', age=91, school='91'}//students======Student{name='null', age=92, school='92'}//students======Student{name='null', age=93, school='93'}//students======Student{name='null', age=0, school='95'}//students======Student{name='null', age=0, school='96'}//students======Student{name='10', age=0, school='101'}private ArrayList<Student> getStudents() {ArrayList<Student> students = new ArrayList<>();students.add(new Student("1", 11, "11"));students.add(new Student("2", 21, "21"));students.add(new Student("2", 22, "22"));students.add(new Student("3", 22, "23"));students.add(new Student("3", 31, "31"));students.add(new Student("4", 41, "41"));students.add(new Student("4", 41, "42"));students.add(new Student("5", 51, "51"));students.add(new Student("6", 61, "61"));students.add(new Student("7", 71, "71"));students.add(new Student("8", 81, "81"));students.add(new Student("8", 81, "82"));students.add(new Student("8", 82, "83"));students.add(new Student("9", 91, "91"));students.add(new Student(null, 92, "92"));students.add(new Student(null, 93, "93"));students.add(new Student(null, 93, "94"));students.add(new Student(null, 0, "95"));students.add(new Student(null, 0, "96"));students.add(new Student("10", 0, "101"));return students;}
public class Student {private String name;private int age;private String school;public Student(String name, int age, String school) {this.name = name;this.age = age;this.school = school;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getSchool() {return school;}public void setSchool(String school) {this.school = school;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", school='" + school + '\'' +'}';}
}

ofType : 类型过滤

@Test
public void ofType1() {Disposable disposable = Observable.just("first", 2d, 3L, "four", 5, false).ofType(Integer.class)//只需要Integer.subscribe(new Consumer<Integer>() {@Overridepublic void accept(Integer integer) {System.out.println("integer=============" + integer);}});
}
//integer=============5//类型过滤
@Test
public void ofType2() {Object[] data = {"first", "2", 3, "four", 5, 6, 7};Disposable disposable = Observable.fromArray(data).ofType(Integer.class).filter(new Predicate<Integer>() {@Overridepublic boolean test(Integer integer) throws Exception {return integer > 5;}}).subscribe(new Consumer<Integer>() {@Overridepublic void accept(Integer integer) throws Exception {System.out.println("integer=============" + integer);}});
}
//integer=============6
//integer=============7

buffer : 缓存

buffer:隔m(skip)个数取n(count)个数

“buffer”允许您收集值并以批量形式获取它们,而不是一次收集一个值。它们是缓冲值的几种不同方式。

@Test
public void buffer1() {Disposable disposable = Observable.range(1, 10).buffer(2)//每次take两个.subscribe(System.out::println);
}
//[1, 2]
//[3, 4]
//[5, 6]
//[7, 8]
//[9, 10]@Test
public void buffer2() {//skip分组[1,2,3][4,5,6][7,8,9][10],再take取值//当count < skip,元素被排除在外Disposable disposable = Observable.range(1, 10).buffer(2, 3)//每次skip三个,只take两个.subscribe(System.out::println);
}
//[1, 2]
//[4, 5]
//[7, 8]
//[10]@Test
public void buffer3() {//skip分组[1,2][3,4][5,6][7,8][9,10],再take取值//当count > skip,缓冲区重叠Disposable disposable = Observable.range(1, 10).buffer(3, 2).subscribe(System.out::println);}
//[1, 2, 3]
//[3, 4, 5]
//[5, 6, 7]
//[7, 8, 9]
//[9, 10]@Test
public void buffer4() {//skip分组[1,2][3,4][5,6][7,8][9,10],再take取值Disposable disposable = Observable.range(1, 10)//count : 每个缓冲区应发出的最大大小//skip : 开始新的缓冲区之前,应跳过源ObservableSource发出的多少项。// 请注意,当{@code skip}和{@code count}相等时,// 此操作与 {@link #buffer(int)}相同。.buffer(2, 2).subscribe(System.out::println);
}
//[1, 2]
//[3, 4]
//[5, 6]
//[7, 8]
//[9, 10]@Test
public void buffer5() {PublishSubject<String> subject = PublishSubject.create();Disposable disposable = subject.buffer(3)//获取三个为一组发送.subscribe(new Consumer<List<String>>() {@Overridepublic void accept(List<String> stringList) throws Exception {StringBuilder content = new StringBuilder();for (String s : stringList) {content.append(s).append(",");}System.out.println("content=======" + content);}});subject.onNext("1");subject.onNext("2");subject.onNext("3");subject.onNext("4");subject.onNext("5");subject.onNext("6");subject.onNext("7");subject.onNext("8");subject.onNext("9");subject.onNext("10");subject.onComplete();
}
//content=======1,2,3,
//content=======4,5,6,
//content=======7,8,9,
//content=======10,

这篇关于rxjava : 过滤操作符:filter(条件过滤)、 distinct(去重)、ofType(类型过滤)、buffer(缓存)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/526673

相关文章

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

SpringBoot应用中出现的Full GC问题的场景与解决

《SpringBoot应用中出现的FullGC问题的场景与解决》这篇文章主要为大家详细介绍了SpringBoot应用中出现的FullGC问题的场景与解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录Full GC的原理与触发条件原理触发条件对Spring Boot应用的影响示例代码优化建议结论F

springboot项目中常用的工具类和api详解

《springboot项目中常用的工具类和api详解》在SpringBoot项目中,开发者通常会依赖一些工具类和API来简化开发、提高效率,以下是一些常用的工具类及其典型应用场景,涵盖Spring原生... 目录1. Spring Framework 自带工具类(1) StringUtils(2) Coll

SpringBoot条件注解核心作用与使用场景详解

《SpringBoot条件注解核心作用与使用场景详解》SpringBoot的条件注解为开发者提供了强大的动态配置能力,理解其原理和适用场景是构建灵活、可扩展应用的关键,本文将系统梳理所有常用的条件注... 目录引言一、条件注解的核心机制二、SpringBoot内置条件注解详解1、@ConditionalOn

通过Spring层面进行事务回滚的实现

《通过Spring层面进行事务回滚的实现》本文主要介绍了通过Spring层面进行事务回滚的实现,包括声明式事务和编程式事务,具有一定的参考价值,感兴趣的可以了解一下... 目录声明式事务回滚:1. 基础注解配置2. 指定回滚异常类型3. ​不回滚特殊场景编程式事务回滚:1. ​使用 TransactionT

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

Spring LDAP目录服务的使用示例

《SpringLDAP目录服务的使用示例》本文主要介绍了SpringLDAP目录服务的使用示例... 目录引言一、Spring LDAP基础二、LdapTemplate详解三、LDAP对象映射四、基本LDAP操作4.1 查询操作4.2 添加操作4.3 修改操作4.4 删除操作五、认证与授权六、高级特性与最佳

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim