java - Find the hierarchy -


let's consider class in java

class entity  {  integer id; integer parentid;  public integer getid() {     return id; }  public void setid(integer id) {     this.id = id; }  public integer getparentid() {     return parentid; }  public void setparentid(integer parentid) {     this.parentid = parentid; }   }  } 

consider parentid foreign key(relates id object).

now created 6 objects , put values.

entity e1 = new entity();     e1.setid(400);      entity e2 = new entity();     e2.setid(300);             e2.setparentid(400);      entity e3 = new entity();     e3.setid(200);     e3.setparentid(300);      entity e4 = new entity();     e4.setid(100);             e4.setparentid(200);      entity e5 = new entity();     e5.setid(50);             e5.setparentid(100);      entity e6 = new entity();     e6.setparentid(50); 

now want obtain hierarchy of objects. means if give id, should complete parent hierarchy , child hierarchy.

for eg: if give 100 id(entity: e4), should parent hierarchy:- e4,e3,e2,e1 child hierarchy:- e4,e5,e6

explanation:- parent hierarchy:- should add initial e4 object first. find object id same of e4's parentid.(here e3) process continues untill, parentid null child hierarchy:- should add initial e4 object first. find object parentid same of e4's id. (here e5)the process continues untill, parentid null

solution me parent hierarchy:-

 list<entity> parent = new arraylist<entity>();      entity ent = list.stream().filter(e -> e.getid() == 100).findfirst()             .get(); // // 100 input id value      parent.add(ent);      integer parentid = ent.getparentid();      while (parentid != null) {          int search = parentid;         entity newentity = list.stream().filter(e -> e.getid() == search)                 .findfirst().get();          parent.add(newentity);         parentid = newentity.getparentid();     } 

for child hierarchy:

    entity entnew = list.stream().filter(e -> e.getid() == 100).findfirst()             .get(); // 100 input id value        child.add(entnew);       integer idnew = entnew.getid();       while (idnew != null) {      int searchnew = idnew;      entity newent = list.stream().filter(f -> f.getparentid()!= null && f.getparentid() == searchnew)             .findfirst().get();      child.add(newent);     idnew = newent.getid();      } 

i found method solve scenario, want more efficent solution in java 8 using core concepts solve this.

i've found more java8-ish solution, smell of functional programming.

given 6 entities (please note i've set id e6, otherwise nullpointerexception):

entity e1 = new entity(); e1.setid(400);  entity e2 = new entity(); e2.setid(300); e2.setparentid(400);  entity e3 = new entity(); e3.setid(200); e3.setparentid(300);  entity e4 = new entity(); e4.setid(100); e4.setparentid(200);  entity e5 = new entity(); e5.setid(50); e5.setparentid(100);  entity e6 = new entity(); e6.setid(25); // id must set, or we'll npe e6.setparentid(50); 

and list containing them:

list<entity> list = new arraylist<>(); list.add(e1); list.add(e2); list.add(e3); list.add(e4); list.add(e5); list.add(e6); 

then, parents hierarchy:

function<integer, entity> byid =      id -> list.stream()         .filter(e -> e.getid().equals(id))         .findfirst()         .orelse(null);  entity parentsseed = byid.apply(100); // e4  unaryoperator<entity> nextparent =      e -> e == null ? e : byid.apply(e.getparentid());  list<entity> parents =      stream.iterate(parentsseed, nextparent)         .limit(list.size())         .filter(objects::nonnull)         .collect(collectors.tolist()); // [e4, e3, e2, e1] 

and children hierarchy:

entity childrenseed = byid.apply(100); // e4  function<integer, entity> byparentid =      id -> list.stream()         .filter(e -> id.equals(e.getparentid()))         .findfirst()         .orelse(null);  unaryoperator<entity> nextchild =      e -> e == null ? e : byparentid.apply(e.getid());  list<entity> children =      stream.iterate(childrenseed, nextchild)         .limit(list.size())         .filter(objects::nonnull)         .collect(collectors.tolist()); // [e4, e5, e6] 

the idea use stream.iterate() method, creating stream means of "functional" iteration.

for parents, i've created unaryoperator (a function) that, given entity, returns either parent entity or null; children, i've created unaryoperator that, given entity, returns either child entity or null.

to perform 2 searches, i've used function searches list id , parentid, respectively.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -