- Spring 5.0 Projects
- Nilang Patel
- 255字
- 2021-07-02 12:35:03
Observable for iterators
Observable provides support to emit the data from any iterable sources, for example, lists, maps, sets, and so on. It will call onNext() on each item of an iterable type, and once the iterator is over, it will call onComplete() automatically. Iterable in Java is commonly used in collection frameworks, so Observable with iterable can be used while fetching data from collection classes.
Let's see how to use it as follows:
public class RxJavaIterableDemo {
public static void main(String[] args) {
List<EmployeeRating> employeeList = new ArrayList<EmployeeRating>();
EmployeeRating employeeRating1 = new EmployeeRating();
employeeRating1.setName("Lilly");
employeeRating1.setRating(6);
employeeList.add(employeeRating1);
employeeRating1 = new EmployeeRating();
employeeRating1.setName("Peter");
employeeRating1.setRating(5);
employeeList.add(employeeRating1);
employeeRating1 = new EmployeeRating();
employeeRating1.setName("Bhakti");
employeeRating1.setRating(9);
employeeList.add(employeeRating1);
employeeRating1 = new EmployeeRating();
employeeRating1.setName("Harmi");
employeeRating1.setRating(9);
employeeList.add(employeeRating1);
Observable<EmployeeRating> employeeRatingSource =
Observable.fromIterable(employeeList);
employeeRatingSource.filter(employeeRating ->
employeeRating.getRating() >=7).subscribe(empRating ->
System.out.println("Star Employee: " + empRating.getName()
+ " Rating : "+empRating.getRating()));
}
}
We are populating the list of EmployeeRating and creating Observable with thefromIterable() method by passing this list. The class EmployeeRating is a simple POJO containing the name and rating attributes as follows:
class EmployeeRating{
private String name;
private int rating;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRating() {
return rating;
}
public void setRating(int rating) {
this.rating = rating;
}
}
RxJava conforms to Reactive Streams specification by providing an implementation of interfaces. Let's recall that the onNext(), onError(), onSubscribe(), and onComplete() methods are part of the Observer interface. RxJava provides an implementation of these interfaces to handle respective events.
- 社交網(wǎng)絡(luò)對齊
- RCNP實(shí)驗(yàn)指南:構(gòu)建高級(jí)的路由互聯(lián)網(wǎng)絡(luò)(BARI)
- Twilio Cookbook
- 物聯(lián)網(wǎng)+BIM:構(gòu)建數(shù)字孿生的未來
- 物聯(lián)網(wǎng)通信技術(shù)
- WordPress Web Application Development
- Working with Legacy Systems
- Master Apache JMeter:From Load Testing to DevOps
- 語音信號(hào)處理及Blackfin DSP實(shí)現(xiàn)
- 轉(zhuǎn)化:提升網(wǎng)站流量和轉(zhuǎn)化率的技巧
- 物聯(lián)網(wǎng)導(dǎo)論
- 5G新型多址技術(shù)
- SD-WAN 架構(gòu)與技術(shù)
- 賽博空間簡史
- 深入理解Kubernetes網(wǎng)絡(luò)系統(tǒng)原理