将元素添加到RxJava中的可观察列表

时间:2016-01-31 12:45:33

标签: java java-8 reactive-programming rx-java observable

嗨,我是RxJava的初学者,并且学习了我在下面做过的事情,

ArrayListObserver.java

import rx.functions.Action1;
public class ArrayListObserver implements Action1<Object>{
    public void call(Object element) {
        System.out.println(element);
    }
}

ArrayListWithObservableDemo.java

import java.util.Arrays;
import java.util.List;
import rx.Observable;
import rx.functions.Action1;
public class ArrayListWithObservableDemo {
    List<String> list;
    Observable<String> observableList;
    public void createList(){
        list = Arrays.asList("One", "Two", "Three", "Four", "Five");
    }
    public void createObservableList(){
        observableList = Observable.from(list);
    }
    public void addObserver(Action1<Object> action1){
        observableList.subscribe(action1);
    }
    public void addtoList(String string){
        list.add(string);
    }
}

TestApp.java

public class TestApp {
    public static void main(String[] args) throws InterruptedException {
        ArrayListWithObservableDemo demo = new ArrayListWithObservableDemo();
        demo.createList();
        demo.createObservableList();
        demo.addObserver(new ArrayListObserver());
        Thread.sleep(5000);
        demo.addtoList("Six");
        Thread.sleep(3000);
    }
}

现在在运行主要版本时,我遇到以下错误: list.add(string);

java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(Unknown Source)
    at java.util.AbstractList.add(Unknown Source)
    at org.demo.app.ArrayListWithObservableDemo.addtoList(ArrayListWithObservableDemo.java:27)
    at org.demo.app.TestApp.main(TestApp.java:11)

是否可以使用新元素添加可观察列表,或者我缺少一些反应性编程的基本概念,如果是这样,请建议我一些好的链接/示例来理解java中反应性思维的概念

由于

0 个答案:

没有答案