关于容器的理解

来源:互联网 发布:河北大学网络教育 编辑:程序博客网 时间:2024/05/16 08:15
public class MainTest {static List<B> as = new ArrayList<B>();static B a1 = new B(1, 0, "b1");static B a2 = new B(2, 1, "b2");static B a3 = new B(3, 2, "b3");static B a4 = new B(4, 2, "b4");public static void main(String args[]) {as.add(a3);as.add(a2);as.add(a4);getB(as);System.out.println(a1);}public static void getB(List<B> as) {Map<Integer, List<B>> map = as.stream().collect(Collectors.groupingBy(B::getPid));as.add(a1);for (Map.Entry<Integer, List<B>> entry : map.entrySet()) {as.stream().filter(x -> x.getId() == entry.getKey()).findAny().get().setB(entry.getValue());}}}

public class B {private int id;@Overridepublic String toString() {return "B [id=" + id + ", pid=" + pid + ", name=" + name + ", b=" + b + "]";}private int pid;private String name;private List<B> b;public int getId() {return id;}public void setId(int id) {this.id = id;}public int getPid() {return pid;}public void setPid(int pid) {this.pid = pid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public List<B> getB() {return b;}public B setB(List<B> b) {this.b = b;return this;}public B(int id, int pid, String name, List<B> b) {super();this.id = id;this.pid = pid;this.name = name;this.b = b;}public B(int id, int pid, String name) {super();this.id = id;this.pid = pid;this.name = name;}}


容器中存的是地址而非实体

0 0