title: Android LiveData Transformation by changing list type
body: <p>I have this view model</p>
  
  <pre><code>public class MainViewModel extends AndroidViewModel {
  
          // Constant for logging
          private static final String TAG = MainViewModel.class.getSimpleName();
  
          private LiveData&lt;List&lt;JournalEntry&gt;&gt; journals;
  
          public MainViewModel(Application application) {
              super(application);
              AppDatabase database = AppDatabase.getInstance(this.getApplication());
              Log.d(TAG, "Actively retrieving the tasks from the DataBase");
              journals = database.journalDao().loadAllJournals();
          }
  
          public LiveData&lt;List&lt;JournalEntry&gt;&gt; getJournals() {
              return journals;
          }
      }
  </code></pre>
  
  <p>which returns a <code>LiveData&lt;List&lt;JournalEntry&gt;&gt;</code> I would like to transform the live data to return a <code>LiveData&lt;List&lt;ListItem&gt;&gt;</code> where the ListItem  list contains JournalEntry objects, and DateHeader Objects</p>
  
  <p>I had tried to manipulate the observing list like this</p>
  
  <pre><code>private void setupViewModel() {
          MainViewModel viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
          viewModel.getJournals().observe(this, new Observer&lt;List&lt;JournalEntry&gt;&gt;() {
              @Override
              public void onChanged(@Nullable List&lt;JournalEntry&gt; journalEntries) {
                  Log.d(TAG, "Updating list of tasks from LiveData in ViewModel");
  
                  Map&lt;Date, List&lt;JournalEntry&gt;&gt; journals = toMap(journalEntries);
                  Date previousDate = null;
                  for (Date date : journals.keySet()) {
                      HeaderItem header = new HeaderItem(date);
                      Date currentDate = header.getDate();
                      if(previousDate==null || !DateUtil.formatDate(currentDate).equals(DateUtil.formatDate(previousDate))){
                          items.add(header);
                      }
  
                      for (JournalEntry journal : journals.get(date)) {
                          JournalItem item = new JournalItem(journal);
                          items.add(item);
                          previousDate = item.getJournalItem().getCreatedAt();
                      }
                  }
                  mAdapter.setItems(items);
              }
          });
      }
  </code></pre>
  
  <p>But realized the view model is duplicating all the items onChange instead of just updating the ones that have changed. I am not quite sure how I can achieve this using LiveData transfromation</p>
  
  <p>Thanks in Advance </p>