title: Sort and merge two Arraylist of user defined type Library class
body: <p><strong>Aim :</strong> I have to two sorted Arraylists of OSD Type(class part of an library inported). I need to merge them to create final sorted list(soerted by time in descending order) in the most optimized way.</p>
  
  <blockquote>
    <p><strong>Problem</strong> : I can't modify the user defined OSD class as its part
    of an  library and hence I cant edit and make it implements Comparable
    class.    <strong><em>I have already created the function which performs the task,
    but it I want to optimize the code and make it less expensive</em></strong>.</p>
  </blockquote>
  
  <p>I have come across many solutions that involve implementing Comparable,which I can't do.</p>
  
  <p>Current working <strong>code</strong> that I want to optimize for better performance</p>
  
  <pre><code>private ArrayList&lt;OSD&gt; latestFistSortMergeOsds(
              ArrayList&lt;OSD&gt; awbThingTypeOSDs,
              ArrayList&lt;OSD&gt; LDDthingTypeOSDs) {
          ArrayList&lt;OSD&gt; sortedOsds = new ArrayList&lt;OSD&gt;();
          ArrayList&lt;Long&gt; timestampList = null;
  
          HashMap&lt;Long, OSD&gt; awbThingTypeOSDsHmap = null;
          if (awbThingTypeOSDs != null) {
              awbThingTypeOSDsHmap = new HashMap&lt;Long, OSD&gt;();
              for (OSD mOSD : awbThingTypeOSDs) {
  
                  awbThingTypeOSDsHmap.put(mOSD.getUpdatedOn()
                          .getTime(), mOSD);
              }
              Log.i("sorted", "hmap" + awbThingTypeOSDsHmap);
          }
          HashMap&lt;Long, OSD&gt; LDDthingTypeOSDsHmap = null;
          if (LDDthingTypeOSDs != null) {
              LDDthingTypeOSDsHmap = new HashMap&lt;Long,OSD&gt;();
              for (OSD nOSD : LDDthingTypeOSDs) {
  
                  LDDthingTypeOSDsHmap.put(nOSD.getUpdatedOn()
                          .getTime(), nOSD);
              }
          }
          // merge n sort timestamp
          if (awbThingTypeOSDsHmap != null) {
              timestampList = new ArrayList&lt;Long&gt;(awbThingTypeOSDsHmap.keySet());
  
              if (LDDthingTypeOSDsHmap != null) {
                  ArrayList&lt;Long&gt; timestampListLDD = new ArrayList&lt;Long&gt;(
                          LDDthingTypeOSDsHmap.keySet());
                  timestampList.addAll(timestampListLDD);
              }
              Collections.sort(timestampList, Collections.reverseOrder());// descending
              Log.i("sorted", "sorted keyList" + timestampList);
          }
  
          // merge sorted osds- latest first
          // timestamplist is sorted in desc order
          if (timestampList != null) {
              for (Long timestampKey : timestampList) {
                  RareMediaCompanyOSD osd = null;
  
                  osd = awbThingTypeOSDsHmap.get(timestampKey);
                  if (osd == null) {
                      osd = LDDthingTypeOSDsHmap.get(timestampKey);
                  }
  
                  sortedOsds.add(osd);
  
              }
          }
  
          return sortedOsds;
      }
  </code></pre>
  
  <p>I have referred to the following links
  <a href="https://stackoverflow.com/questions/6176019/comparing-long-values-using-collections-sortobject">Comparing Long values using Collections.sort(object)</a></p>
  
  <p><a href="https://stackoverflow.com/questions/6176019/comparing-long-values-using-collections-sortobject">Comparing Long values using Collections.sort(object)</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5894818/how-to-sort-arraylistlong-in-java-in-decreasing-order">How to sort ArrayList&lt;Long&gt; in Java in decreasing order?</a></p>
  
  <p><strong>Please suggest better usage of the Hashmaps or should I use some other DS instead of hashmap to optimize the code.</strong></p>
  
  <p>P.S : its not a duplicate of these questions as I cannot edit the class and implement comparable,as its a part of library.</p>
  
  <p>Thanks!</p>