Search code examples
javarx-java2

Unable to subscribe using group by


I have tried many examples but it just didn't work out for me, I simply want to receive the grouped results as list using the following code:

  private final PublishSubject<MyObject> s;

  public RabbitMQConsumer() {
    s = PublishSubject.create();
    s.groupBy(x -> x.getName())
    .flatMapSingle(x -> x.toList())
    .subscribe(x -> System.out.println(x));
  }

This should work as the example says, however it never prints out anything. If I remove groupby it starts to print out the incoming values.

Any ideas what I am doing wrong?


Solution

  • I did a sample code for your code and works fine

    List<String> list = Arrays.asList("AN", "BL", "CL", "DO", "AK", "LL", "BO", "DL");
    Observable.fromIterable(list).groupBy(x -> x.charAt(0))
            .flatMapSingle(x -> x.toList())
            .subscribe(x -> System.out.println(x));
    

    Output as follow:

    [AN, AK]
    [BL, BO]
    [CL]
    [DO, DL]
    [LL]
    

    Why not override onError() to see if there's an error occurs