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

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

深入探索协同过滤:从原理到推荐模块案例

文章目录 前言一、协同过滤1. 基于用户的协同过滤(UserCF)2. 基于物品的协同过滤(ItemCF)3. 相似度计算方法 二、相似度计算方法1. 欧氏距离2. 皮尔逊相关系数3. 杰卡德相似系数4. 余弦相似度 三、推荐模块案例1.基于文章的协同过滤推荐功能2.基于用户的协同过滤推荐功能 前言     在信息过载的时代,推荐系统成为连接用户与内容的桥梁。本文聚焦于

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听