Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava2 : Filtering a List<Object> in an Observable

I am very new to Rx programming and I was trying my hands on it in a project. What I want is I am having an object of GamesInfoList as shown below:

@AutoValue
public abstract class GameInfoList {
    public static TypeAdapter<GameInfoList> typeAdapter(Gson gson) {
        return new AutoValue_GameInfoList.GsonTypeAdapter(gson);
    }

    public static Builder builder() {
        return new AutoValue_GameInfoList.Builder();
    }

    public abstract long id();

    @Nullable
    public abstract String date_added();

    @Nullable
    public abstract String date_last_updated();

    @Nullable
    public abstract GameImages image();

    @Nullable
    public abstract String name();

    @Nullable
    public abstract List<GamePlatformInfo> platforms();

    @AutoValue.Builder
    public abstract static class Builder {

        public abstract Builder id(long id);

        public abstract Builder date_added(String date_added);

        public abstract Builder date_last_updated(String date_last_updated);

        public abstract Builder image(GameImages image);

        public abstract Builder name(String name);

        public abstract Builder platforms(List<GamePlatformInfo> platforms);

        public abstract GameInfoList build();
    }
}

Now, I want to load my games by date and name and I wrote a method for that to access this above object in it.

public void loadGamesByDateAndName(String date, String name) {
    Timber.d("Load games by date: " + date + " and name: " + name);
    remoteDataHelper
            .getGamesListByDate(date)
            .flatMapObservable(Observable::fromIterable)
            .filter(game -> game.platforms() != null)
            .filter(game -> game.platforms().name().contains(name))
            .toList()
            .subscribe(getObserverFiltered(name, false));
}

Here, I am filtering (using filter() operator) the platforms object based on the name inputted in this method. The above method is showing me an error because platform is of type List. How can I filter through the list to match the name and display in my Android app? What changes need to be made to the above method in order to load data by date and name?

like image 421
Nithin Prasad Avatar asked Aug 23 '26 01:08

Nithin Prasad


1 Answers

The simplest way is to iterate inside the second filter operator.

public void loadGamesByDateAndName(String date, String name) {
    Timber.d("Load games by date: " + date + " and name: " + name);
    remoteDataHelper
            .getGamesListByDate(date)
            .flatMapObservable(Observable::fromIterable)
            .filter(game -> game.platforms() != null)
            .filter(game -> { for(GamePlatformInfo platform : game.platforms()){
                                  if(platform.name().contains(name)) {
                                      return true;
                                  }
                              }
                              return false;
                    }
            )
            .toList()
            .subscribe(getObserverFiltered(name, false));
}
like image 147
kenny_k Avatar answered Aug 26 '26 08:08

kenny_k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!