Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tr2;
- import java.util.ArrayList;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- public class Alta {
- static Map<Long, Linie> m = new ConcurrentHashMap<Long, Linie>();
- public static void main(String[] args) {
- Response response = new Response();
- populateMap();
- // afiseazaMap();
- Long actual = getFirstAncestor(m);
- response.setId(actual);
- response.setName(m.get(actual).getName());
- response.setChildren(takeTree(actual, m));
- System.out.println(response);
- Gson gs = new GsonBuilder().setPrettyPrinting().create();
- String jso = gs.toJson(response);
- System.out.println(jso);
- }
- private static ArrayList<Response> takeTree(Long actual,
- Map<Long, Linie> map) {
- System.out.println("[takeTree] actual = " + actual);
- ArrayList<Response> al = new ArrayList<Response>();
- for (Long key : map.keySet()) {
- Linie l = map.get(key);
- if (l != null && l.getParentId() != null && l.getParentId().equals(actual)) {
- Response r = new Response();
- r.setId(l.getId());
- r.setName(l.getName());
- r.setChildren(takeTree(l.getId(), map));
- al.add(r);
- }
- }
- return al;
- }
- private static Long getFirstAncestor(Map<Long, Linie> m) {
- for (Long i : m.keySet()) {
- return m.get(i).getUltimateId();
- }
- return null;
- }
- public static void afiseazaMap(Map<Long, Linie> m) {
- for (Long key : m.keySet()) {
- Linie l = m.get(key);
- System.out.println(l.getId() + " --- " + l.getParentId() + " --- "
- + l.getUltimateId() + " --- " + l.getName());
- }
- }
- public static void populateMap() {
- add(1, 3, 5);
- add(2, 3, 5);
- add(3, 5, 5);
- add(4, 5, 5);
- m.put((long) 5, new Linie(new Long(5), null, new Long(5)));
- }
- public static void add(long i, long j, long k) {
- m.put((long) i, new Linie(i, j, k));
- }
- }
- class Linie {
- Long id;
- Long parentId;
- Long ultimateId;
- String name = "unNume";
- public Linie(long id, long parentId, long ultimateId) {
- this.id = id;
- this.parentId = parentId;
- this.ultimateId = ultimateId;
- this.name = this.name + id;
- }
- public Linie(Long id, Long parentId, Long ultimateId) {
- this.id = id;
- this.parentId = parentId;
- this.ultimateId = ultimateId;
- this.name = this.name + id;
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public Long getParentId() {
- return parentId;
- }
- public void setParentId(Long parentId) {
- this.parentId = parentId;
- }
- public Long getUltimateId() {
- return ultimateId;
- }
- public void setUltimateId(Long ultimateId) {
- this.ultimateId = ultimateId;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- ---------------------
- package tr2;
- import java.io.Serializable;
- import java.util.ArrayList;
- public class Response implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- Long id;
- String name;
- ArrayList<Response> children;
- @Override
- public String toString(){
- return "id = " + id + "; name = " + name + " children = \n " + children;
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public ArrayList<Response> getChildren() {
- return children;
- }
- public void setChildren(ArrayList<Response> children) {
- this.children = children;//new ArrayList(children);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement