title: RxJava : if/else operation in flatMap
body: <p>I try to get and render some data like below</p>
  
  <p>raw data class</p>
  
  <pre><code>  @Data @AllArgsConstructor class Category {
      String name;
      List&lt;String&gt; items;
    }
  </code></pre>
  
  <p>presentation class</p>
  
  <pre><code>  @Data @AllArgsConstructor class ViewModel {
      public static final int TYPE_HEADER = 0;
      public static final int TYPE_ITEM = 1;
      int type;
      String category;
      String itemName;
    }
  </code></pre>
  
  <p>and below code is request and transform subscribed data to presentation object.</p>
  
  <pre><code>  Observable.create(new Observable.OnSubscribe&lt;Category&gt;() {
        @Override public void call(Subscriber&lt;? super Category&gt; subscriber) {
          subscriber.onNext(new Category("1", Lists.newArrayList("", "a", "b")));
          subscriber.onNext(new Category("2", Lists.newArrayList("")));// this data does not output
          subscriber.onNext(new Category("3", Lists.newArrayList("c", "", "d")));
          subscriber.onNext(new Category("4", Lists.newArrayList("e", "f", "")));
        }
      }).flatMap(new Func1&lt;Category, Observable&lt;ViewModel&gt;&gt;() {
        @Override public Observable&lt;ViewModel&gt; call(Category category) {
  
          // TODO make this block to one line
  
          // 1. clean response data and transform to ViewModel    
          List&lt;ViewModel&gt; cleanedItems = Lists.newArrayList(
              Observable.from(category.getItems()).filter(new Func1&lt;String, Boolean&gt;() {
                @Override public Boolean call(String s) {
                  return s != null &amp;&amp; !s.isEmpty();
                }
              }).map(new Func1&lt;String, ViewModel&gt;() {
                @Override public ViewModel call(String item) {
                  return new ViewModel(ViewModel.TYPE_ITEM, null, item);
                }
              }).toBlocking().toIterable());
  
          if (cleanedItems.isEmpty()) {
            // 2. case : skip
            return Observable.empty();
          } else {
            // 3. case : add header and cleaned data
            return Observable.concat(
                Observable.just(new ViewModel(ViewModel.TYPE_HEADER, category.getName(), null)),
                Observable.from(cleanedItems));
          }
        }
      }).subscribe(new Action1&lt;ViewModel&gt;() {
        @Override public void call(ViewModel viewModel) {
          // render data
          System.out.println(viewModel.toString());
        }
      });
  </code></pre>
  
  <p>output</p>
  
  <pre><code>ViewModel(type=0, category=1, itemName=null)
  ViewModel(type=1, category=null, itemName=a)
  ViewModel(type=1, category=null, itemName=b)
  ViewModel(type=0, category=3, itemName=null)
  ViewModel(type=1, category=null, itemName=c)
  ViewModel(type=1, category=null, itemName=d)
  ViewModel(type=0, category=4, itemName=null)
  ViewModel(type=1, category=null, itemName=e)
  ViewModel(type=1, category=null, itemName=f)
  </code></pre>
  
  <p>I try to write 1, 2, 3 (in comment) statement to 1 line (or more readable way), but I had no idea.
  <code>defaultIfEmpty</code> operator seems not to use in this case.</p>
  
  <p>Do anyone  have any idea ?</p>