Java的孩子认识父母,但父母不认识他们的孩子

时间:2018-10-31 10:07:47

标签: java

我有一个Java对象列表。例如,以下类似于JSON的伪代码:

[
  {
    username: "u1",
    password: "p1",
    email: "e1",
    parent: null
  },
  {
    username: "u2",
    password: "p2",
    email: "e2",
    parent: (the first object for example)
  },
  {
    username: "u3",
    password: "p3",
    email: "e3",
    parent: (also the first object for example)
  }
]

我想要一个将所有对象组织成这样的数组的函数:

[
  {
    {
      username: "u1",
      password: "p1",
      email: "e1",
      parent: null
    },
    [
      {
        username: "u2",
        password: "p2",
        email: "e2",
        parent: (as the parent of u2 is u1 it will be nested inside him)
      },
      {
        username: "u3",
        password: "p3",
        email: "e3",
        parent: (also as the parent of u3 is u1 it will be nested inside him)
      }
    ]
  }
]

基本上,我需要一个搜索所有子对象并将其嵌套在其对应父对象中的函数。

我尝试在Google中搜索,但无法向他表达自己的想法。有人可以给些提示或帮助吗?

PS:在此示例中,为了更易于理解,我使用JSON编写了对象,但它们使用的是Java。该对象通过指向另一个对象的“父”字段知道其父对象

2 个答案:

答案 0 :(得分:1)

如果您使用的是Java8,则可以使用以下代码段:-它基于java8流中的groupingBy功能。还使用了Optional的支持来处理带有null父级的根元素的特殊情况。

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class Client {

    public static void main(String[] args) {
        User user1 = new User("u1", "p1", "e1", null);
        User user2 = new User("u2", "p2", "e2", user1);
        User user3 = new User("u3", "p3", "e3", user1);
        List<User> users = Arrays.asList(user1, user2, user3);

        Collection<List<User>> userGroups = group(users);

        System.out.println(userGroups);
    }

    private static Collection<List<User>> group(List<User> users) {
        Map<Optional<User>, List<User>> userGroups = users.stream()
                .collect(
                        Collectors.groupingBy(user -> Optional.ofNullable(user.getParent()))
                        );

        return userGroups.values();

    }
}

class User {

    String username;
    String password;
    String email;
    User parent;

    public User(String uname, String pword, String email, User parent) {
        this.username = uname;
        this.password = pword;
        this.email = email;
        this.parent = parent;
    }

    public User getParent() {
        return parent;
    }

    public void setParent(User parent) {
        this.parent = parent;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((email == null) ? 0 : email.hashCode());
        result = prime * result + ((password == null) ? 0 : password.hashCode());
        result = prime * result + ((username == null) ? 0 : username.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (email == null) {
            if (other.email != null)
                return false;
        } else if (!email.equals(other.email))
            return false;
        if (password == null) {
            if (other.password != null)
                return false;
        } else if (!password.equals(other.password))
            return false;
        if (username == null) {
            if (other.username != null)
                return false;
        } else if (!username.equals(other.username))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "{username:" + username + ", password:" + password + ", email:" + email + "}";
    }   
}

输出如下所示:-

[[{username:u1, password:p1, email:e1}], [{username:u2, password:p2, email:e2}, {username:u3, password:p3, email:e3}]]

答案 1 :(得分:-1)

我建议将子字段添加到您的java类中。然后实现hashCode和equals函数

然后尝试此功能

static List<Item> mapChild(List<Item> items, Item parent) {
    List<Item> result = new ArrayList<>();
    for (Item item: items) {
        if (parent == null ? item.parent == null : item.parent != null && item.parent.equals(parent)) {
            item.child.addAll(mapChild(items, item));
            result.add(item);
        }
    }
    return result;
}
相关问题