distinct: Reduplicate the data in the data source and output it
filter: filter data according to filtering rules
buffer:Nu m ber of intervals n
Code address:
GitHub - GodisGod/Rxjava2Test
https://github.com/GodisGod/Rxjava2Test
distinct:
We have a data source here:
private Integer[] datas = {1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5};
Note that this is an array of Integer objects. If it's an int[], it won't work. You can try it.
We duplicate these numbers and output them
Observable.fromArray(datas)//Note that this is an Integer array. If it's an int[], that's not possible. You can try it.
.distinct()
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer value) throws Exception {
Log.i("LHD", "distinct : " + value);
tvResult.append(value + " ");
}
});
Result:
Try again to filter duplicate data with an object list. Here we need to define which field of the object we want to filter based on. If the fields of the two objects are equal, filter
We have defined a student data source:
Student student1 = new Student("LHD", 90);
Student student2 = new Student("LHD1", 80);
Student student3 = new Student("LHD2", 70);
Student student4 = new Student("LHD3", 96);
Student student2 = new Student("LHD4", 80);
Student student22 = new Student("LHD4", 80);
Student student23 = new Student("LHD4", 80);
Student student3 = new Student("LHD5", 70);
Student student4 = new Student("LHD5", 70);
Observable.fromIterable(students)
.distinct(new Function<Student, String>() {
@Override
public String apply(Student student) throws Exception {
return student.getName();//Filter if two students have the same score
}
})
.subscribe(new Consumer<Student>() {
@Override
public void accept(Student student) throws Exception {
Log.i("LHD", "distinct : " + student.getName());
tvResult.append(student.getName() + " ");
}
});
Output results:
filter: It's easy to use, just look at the code (#^. ^#)
Observable.fromIterable(students)
.filter(new Predicate<Student>() {
@Override
public boolean test(Student student) throws Exception {
return student.getScore() > 80;//Filtering over 80 points
}
}).subscribe(new Consumer<Student>() {
@Override
public void accept(Student student) throws Exception {
Log.i("LHD", " filter: " + student.getName());
tvResult.append("More than 80 points:\n" + student.getName() + "\n");
}
});
Output results:
buffer: output n data every m data
Data Source: Output the first two in every three, as shown in the following figure
Student student1 = new Student("LHD", 90);//---------- Output
Student student2 = new Student("LHD1", 80);//---------- Output
Student student3 = new Student("LHD2", 70);
Student student4 = new Student("LHD3", 96);//---------- Output
Student student2 = new Student("LHD4", 80);//---------- Output
Student student22 = new Student("LHD4", 80);
Student student23 = new Student("LHD4", 80);//---------- Output
Student student3 = new Student("LHD5", 70);//---------- Output
Student student4 = new Student("LHD5", 70);
Observable.fromIterable(students)
.buffer(2, 3)//Take two first, two for each three
.subscribe(new Consumer<List<Student>>() {
@Override
public void accept(List<Student> students) throws Exception {
for (Student s : students) {
Log.i("LHD", " buffer: " + s.getName());
tvResult.append(s.getName() + " " + s.getScore() + "\n");
}
}
});
Output results: