Efficiently compare 2 lists to replace and order












0












$begingroup$


I have 2 list containing some Objects (Fruit class). I am using a 3rd list to add these elements based on following 2 criteria.




  1. I want every object in the 1st list added to 3rd list. But if I have a matching object in the 2nd list (matching based on id and isChecked), I want to add the object from the 2nd list to the 3rd list and ignore the one in 1st list.


  2. If I did the switch mentioned on point one, I want to move that object up to the first element of the 3rd list.



I have it working with following code. But I find it very inefficient. Is there a better way around it?



Bear in mind I have no control over the second list, but the first list is coming from a Rest endpoint and I am currently capturing it as a list. Unsure if I should have opted for a Map. Please advice.



Example:



In the following example, expected list output is [f2, f5, f1, f3, f4] (based on name).



It is cos I have all the elements from the first list. f2 and f5 went in front of the order as they came from second list (they matched elements in first list and had isChecked set to true).



import lombok.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class App {
public static void main(String args) {

Fruit fruit1 = new Fruit("1", "f1", false);
Fruit fruit2 = new Fruit("2", "f2", false);
Fruit fruit3 = new Fruit("3", "f3", false);
Fruit fruit4 = new Fruit("4", "f4", false);
Fruit fruit5 = new Fruit("5", "f5", false);

List<Fruit> firstList = Arrays.asList(fruit1, fruit2, fruit3, fruit4, fruit5);

Fruit fruit6 = new Fruit("2", "f2", true);
Fruit fruit7 = new Fruit("7", "f7", false);
Fruit fruit8 = new Fruit("5", "f5", true);
Fruit fruit9 = new Fruit("9", "f9", false);
Fruit fruit10 = new Fruit("10", "f10", false);

List<Fruit> secondList = Arrays.asList(fruit6, fruit7, fruit8, fruit9, fruit10);

List<Fruit> finalList = new ArrayList<>();

// expected list = [f2, f5, f1, f3, f4]

// this loop is checking and adding objects to finalList.
// must match the first list and isChecked.
// in this case, only f6 and f8 matches the first list (id match) and is also 'checked'.
for (Fruit first : firstList){
for (Fruit second : secondList){
if(first.getId().equals(second.getId()) && second.isChecked()){
finalList.add(second);
break;
}
}
}

// not done yet. Still need to loop and add back the elements from the first list
// which were not added in the above loop
boolean addedFirst = false;
outer:
for(Fruit first : firstList){
for(Fruit finalFruit : finalList){
if(first.getId().equals(finalFruit.getId())){
continue outer;
}
}
finalList.add(first);
}

for(Fruit fruit : finalList){
System.out.println(fruit);
}
}
}

@Getter
@Setter
@ToString
class Fruit{
private String id;
private String name;
private boolean isChecked;

Fruit(String id, String name, boolean isChecked) {
this.id = id;
this.name = name;
this.isChecked = isChecked;
}
}









share|improve this question









$endgroup$

















    0












    $begingroup$


    I have 2 list containing some Objects (Fruit class). I am using a 3rd list to add these elements based on following 2 criteria.




    1. I want every object in the 1st list added to 3rd list. But if I have a matching object in the 2nd list (matching based on id and isChecked), I want to add the object from the 2nd list to the 3rd list and ignore the one in 1st list.


    2. If I did the switch mentioned on point one, I want to move that object up to the first element of the 3rd list.



    I have it working with following code. But I find it very inefficient. Is there a better way around it?



    Bear in mind I have no control over the second list, but the first list is coming from a Rest endpoint and I am currently capturing it as a list. Unsure if I should have opted for a Map. Please advice.



    Example:



    In the following example, expected list output is [f2, f5, f1, f3, f4] (based on name).



    It is cos I have all the elements from the first list. f2 and f5 went in front of the order as they came from second list (they matched elements in first list and had isChecked set to true).



    import lombok.*;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    public class App {
    public static void main(String args) {

    Fruit fruit1 = new Fruit("1", "f1", false);
    Fruit fruit2 = new Fruit("2", "f2", false);
    Fruit fruit3 = new Fruit("3", "f3", false);
    Fruit fruit4 = new Fruit("4", "f4", false);
    Fruit fruit5 = new Fruit("5", "f5", false);

    List<Fruit> firstList = Arrays.asList(fruit1, fruit2, fruit3, fruit4, fruit5);

    Fruit fruit6 = new Fruit("2", "f2", true);
    Fruit fruit7 = new Fruit("7", "f7", false);
    Fruit fruit8 = new Fruit("5", "f5", true);
    Fruit fruit9 = new Fruit("9", "f9", false);
    Fruit fruit10 = new Fruit("10", "f10", false);

    List<Fruit> secondList = Arrays.asList(fruit6, fruit7, fruit8, fruit9, fruit10);

    List<Fruit> finalList = new ArrayList<>();

    // expected list = [f2, f5, f1, f3, f4]

    // this loop is checking and adding objects to finalList.
    // must match the first list and isChecked.
    // in this case, only f6 and f8 matches the first list (id match) and is also 'checked'.
    for (Fruit first : firstList){
    for (Fruit second : secondList){
    if(first.getId().equals(second.getId()) && second.isChecked()){
    finalList.add(second);
    break;
    }
    }
    }

    // not done yet. Still need to loop and add back the elements from the first list
    // which were not added in the above loop
    boolean addedFirst = false;
    outer:
    for(Fruit first : firstList){
    for(Fruit finalFruit : finalList){
    if(first.getId().equals(finalFruit.getId())){
    continue outer;
    }
    }
    finalList.add(first);
    }

    for(Fruit fruit : finalList){
    System.out.println(fruit);
    }
    }
    }

    @Getter
    @Setter
    @ToString
    class Fruit{
    private String id;
    private String name;
    private boolean isChecked;

    Fruit(String id, String name, boolean isChecked) {
    this.id = id;
    this.name = name;
    this.isChecked = isChecked;
    }
    }









    share|improve this question









    $endgroup$















      0












      0








      0





      $begingroup$


      I have 2 list containing some Objects (Fruit class). I am using a 3rd list to add these elements based on following 2 criteria.




      1. I want every object in the 1st list added to 3rd list. But if I have a matching object in the 2nd list (matching based on id and isChecked), I want to add the object from the 2nd list to the 3rd list and ignore the one in 1st list.


      2. If I did the switch mentioned on point one, I want to move that object up to the first element of the 3rd list.



      I have it working with following code. But I find it very inefficient. Is there a better way around it?



      Bear in mind I have no control over the second list, but the first list is coming from a Rest endpoint and I am currently capturing it as a list. Unsure if I should have opted for a Map. Please advice.



      Example:



      In the following example, expected list output is [f2, f5, f1, f3, f4] (based on name).



      It is cos I have all the elements from the first list. f2 and f5 went in front of the order as they came from second list (they matched elements in first list and had isChecked set to true).



      import lombok.*;

      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.List;

      public class App {
      public static void main(String args) {

      Fruit fruit1 = new Fruit("1", "f1", false);
      Fruit fruit2 = new Fruit("2", "f2", false);
      Fruit fruit3 = new Fruit("3", "f3", false);
      Fruit fruit4 = new Fruit("4", "f4", false);
      Fruit fruit5 = new Fruit("5", "f5", false);

      List<Fruit> firstList = Arrays.asList(fruit1, fruit2, fruit3, fruit4, fruit5);

      Fruit fruit6 = new Fruit("2", "f2", true);
      Fruit fruit7 = new Fruit("7", "f7", false);
      Fruit fruit8 = new Fruit("5", "f5", true);
      Fruit fruit9 = new Fruit("9", "f9", false);
      Fruit fruit10 = new Fruit("10", "f10", false);

      List<Fruit> secondList = Arrays.asList(fruit6, fruit7, fruit8, fruit9, fruit10);

      List<Fruit> finalList = new ArrayList<>();

      // expected list = [f2, f5, f1, f3, f4]

      // this loop is checking and adding objects to finalList.
      // must match the first list and isChecked.
      // in this case, only f6 and f8 matches the first list (id match) and is also 'checked'.
      for (Fruit first : firstList){
      for (Fruit second : secondList){
      if(first.getId().equals(second.getId()) && second.isChecked()){
      finalList.add(second);
      break;
      }
      }
      }

      // not done yet. Still need to loop and add back the elements from the first list
      // which were not added in the above loop
      boolean addedFirst = false;
      outer:
      for(Fruit first : firstList){
      for(Fruit finalFruit : finalList){
      if(first.getId().equals(finalFruit.getId())){
      continue outer;
      }
      }
      finalList.add(first);
      }

      for(Fruit fruit : finalList){
      System.out.println(fruit);
      }
      }
      }

      @Getter
      @Setter
      @ToString
      class Fruit{
      private String id;
      private String name;
      private boolean isChecked;

      Fruit(String id, String name, boolean isChecked) {
      this.id = id;
      this.name = name;
      this.isChecked = isChecked;
      }
      }









      share|improve this question









      $endgroup$




      I have 2 list containing some Objects (Fruit class). I am using a 3rd list to add these elements based on following 2 criteria.




      1. I want every object in the 1st list added to 3rd list. But if I have a matching object in the 2nd list (matching based on id and isChecked), I want to add the object from the 2nd list to the 3rd list and ignore the one in 1st list.


      2. If I did the switch mentioned on point one, I want to move that object up to the first element of the 3rd list.



      I have it working with following code. But I find it very inefficient. Is there a better way around it?



      Bear in mind I have no control over the second list, but the first list is coming from a Rest endpoint and I am currently capturing it as a list. Unsure if I should have opted for a Map. Please advice.



      Example:



      In the following example, expected list output is [f2, f5, f1, f3, f4] (based on name).



      It is cos I have all the elements from the first list. f2 and f5 went in front of the order as they came from second list (they matched elements in first list and had isChecked set to true).



      import lombok.*;

      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.List;

      public class App {
      public static void main(String args) {

      Fruit fruit1 = new Fruit("1", "f1", false);
      Fruit fruit2 = new Fruit("2", "f2", false);
      Fruit fruit3 = new Fruit("3", "f3", false);
      Fruit fruit4 = new Fruit("4", "f4", false);
      Fruit fruit5 = new Fruit("5", "f5", false);

      List<Fruit> firstList = Arrays.asList(fruit1, fruit2, fruit3, fruit4, fruit5);

      Fruit fruit6 = new Fruit("2", "f2", true);
      Fruit fruit7 = new Fruit("7", "f7", false);
      Fruit fruit8 = new Fruit("5", "f5", true);
      Fruit fruit9 = new Fruit("9", "f9", false);
      Fruit fruit10 = new Fruit("10", "f10", false);

      List<Fruit> secondList = Arrays.asList(fruit6, fruit7, fruit8, fruit9, fruit10);

      List<Fruit> finalList = new ArrayList<>();

      // expected list = [f2, f5, f1, f3, f4]

      // this loop is checking and adding objects to finalList.
      // must match the first list and isChecked.
      // in this case, only f6 and f8 matches the first list (id match) and is also 'checked'.
      for (Fruit first : firstList){
      for (Fruit second : secondList){
      if(first.getId().equals(second.getId()) && second.isChecked()){
      finalList.add(second);
      break;
      }
      }
      }

      // not done yet. Still need to loop and add back the elements from the first list
      // which were not added in the above loop
      boolean addedFirst = false;
      outer:
      for(Fruit first : firstList){
      for(Fruit finalFruit : finalList){
      if(first.getId().equals(finalFruit.getId())){
      continue outer;
      }
      }
      finalList.add(first);
      }

      for(Fruit fruit : finalList){
      System.out.println(fruit);
      }
      }
      }

      @Getter
      @Setter
      @ToString
      class Fruit{
      private String id;
      private String name;
      private boolean isChecked;

      Fruit(String id, String name, boolean isChecked) {
      this.id = id;
      this.name = name;
      this.isChecked = isChecked;
      }
      }






      java






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 9 hours ago









      Trevor_zamTrevor_zam

      1154




      1154






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211719%2fefficiently-compare-2-lists-to-replace-and-order%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211719%2fefficiently-compare-2-lists-to-replace-and-order%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Список кардиналов, возведённых папой римским Каликстом III

          Deduzione

          Mysql.sock missing - “Can't connect to local MySQL server through socket”