In Java 8, How combine two different collection into one with one matching field? -


i combine 2 different data type collection using 1 matching field.

list<foo> foos... list<bar> bars...   public class foo {     integer fooid;     integer name;     integer description;     integer sent; //this should coming bar     integer received; //this should coming bar }  public class bar {     integer barid;     integer fooid; //combine foo using field     integer sent;     integer received; } 

i data bar put them in foo using fooid. both collection has unique data fooid.

to make code more beautiful, can take @ below :)

list<foo> foos = ... list<bar> bars = ...  // convert list bars map map<integer, bar> map4bar = bars.stream().collect(collectors.tomap(bar::getfooid, function.identity()));  // combine bar foo foos.foreach(foo -> foo.modifybybar(map4bar.get(foo.getfooid())));  

and in foo class there should new method named convert, this:

public foo modifybybar(bar bar){      if(bar != null){         this.sent = bar.sent;         this.received = bar.received;      }      return this;         } 

is code more beautiful? maybe... not, haha~~~^_^


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -