-1

As shown below, I am creating Observables. When I try to apply the faltMap operator i receive compiltation error says:

Cannot infer type argument(s) for <R> flatMap(Function<? super T,? extends ObservableSource<? extends R>>)
Type mismatch: cannot convert from String to ObservableSource<? extends Object>

I know the difference between map and flatMap? I would like to know why when I used the flatMap operator I received the compilation errors.

code:

public static void main(String[] args) {
    Observable<List<Person>> observables = Observable.create(e-> {
        for(List<Person> p : Main.getPersons()) {
            e.onNext(p);
        }
        e.onComplete();
    });
    observables
    //.subscribeOn(Schedulers.newThread())//newThread
    .flatMap(p->p.get(0).getName())
    .map(p->p)
    .doOnNext(p-> System.out.println("p.." + p))
    //.delay(5, TimeUnit.SECONDS)
    //.interval(0, 5, TimeUnit.SECONDS)
    .observeOn(Schedulers.io())
    .subscribe(new Observer() {
        @Override
        public void onComplete() {
            // TODO Auto-generated method stub
            System.out.println("onCompleted");
        }

        @Override
        public void onError(Throwable arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onNext(Object arg0) {
            // TODO Auto-generated method stub
            System.out.println("onNextFromObserver: " + arg0);
        }

        @Override
        public void onSubscribe(Disposable arg0) {
            // TODO Auto-generated method stub
        }
    }); 
}

private static List<List<Person>> getPersons() {
    return Arrays.asList(
            Arrays.asList(new Person("Sanna", 59, "EGY")),
            Arrays.asList(new Person("Mohamed", 69, "EGY")),
            Arrays.asList(new Person("Ahmed", 44, "QTR")),
                    Arrays.asList(new Person("Fatma", 29, "KSA")),
                    Arrays.asList(new Person("Lobna", 24, "EGY"))
                    );
}
}
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • Your issue is unclear to me. Did you state you know the difference but you are unable to tell why you yourself picked `flatMap` over `map` on that line? – akarnokd Nov 02 '18 at 16:46
  • @akarnokd would you please help me to know why flatmap is not used as shown in my code. this will help me understand the difference better – Amrmsmb Nov 02 '18 at 16:54
  • 1
    Do you understand that `String` is not an `ObservableSource`? `flatMap` requires you to return an `ObservableSource` but you return a `String` via `p.get(0).getName()`. Use `map` there! – akarnokd Nov 02 '18 at 16:57

1 Answers1

1

the problem here is p.get(0).getName() should return an observable. the line .map(p->p) is also useless. when you use flatmap you have Observable and in next stream you will have Type1. seems you can use map here instead of flatMap. also after that map you have String so in line below the map function you have to work with String object not the Person object.

can you explain what do you want exactly so I can modify your code?

P.S: so consider map of Class1 to Class2 will emit class2 in downstream but flatMap of Class1 to Observable will emit class3. so if you have observable you need flatmap otherwise use map

Siavash Abdoli
  • 1,852
  • 3
  • 22
  • 38
  • thanks..i just want to make an example to to see the difference between flatmap and map and when should i use them. what i understood so far that.. flatMap: takes stream"Observables, Optional" and return the stream as well. while Map takes stream and return data of the same type of the Streams wraps – Amrmsmb Nov 02 '18 at 19:50
  • in all type of map you can change the type as well. you can have person in upstream and String in downstream if you want. – Siavash Abdoli Nov 02 '18 at 19:56
  • what is the difference between upstream and downstream please – Amrmsmb Nov 02 '18 at 19:57
  • I mean the stream above that line and the stream of data below that line: https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 – Siavash Abdoli Nov 02 '18 at 20:02