获取Java对象中的所有相似对象

huangapple go评论84阅读模式
英文:

To get all similar objects from a Java object

问题

我有一个名为Activity的Java类它附有几个对象
class Activity{
  int id;
  Book book;
  Address address;
  Man man;
  Woman woman;
}
class Man{
   String name;
   List<Address> address;
   List<Appointee> appointees;
}
class Woman{
    String name;   
    List<Phone> phones;
    List<Address> address;
    Manager manager;
}
class Manager{
   String position;
   Address address;
}
class Appointee{
  String type;
  List<Address> address;
}
注意Activity主类是使用JAXB从xsd模式创建的我不能在其中添加任何额外的方法
因此正如可以看出的那样当创建Activity对象时它具有不同区域中类型为Address的对象Activity类具有一个Address类的对象而在manwomanmanager和appointee内部都有属于Address类的对象列表是否有任何方法可以使用流功能或Apache CommonUtils功能从Activity类获取所有这些Address对象否则我需要遍历整个Activity对象并获取Address对象
英文:

I have a java class names Activity. It has several objects attached.

class Activity{
  int id;
  Book book;
  Address address;
  Man man;
  Woman woman;

}

class Man{
   String name;
   List&lt;Address&gt; address;
   List&lt;Appointee&gt; appointees;
}

class Woman{
    String name;   
    List&lt;Phone&gt; phones;
    List&lt;Address&gt; address;
    Manager manager;
}

class Manager{
   String position;
   Adddress address;
}

class Appointee{
  String type;
  List&lt;Address&gt; address;
}

Note that Activity main class is created from an xsd schmea using jaxb. I cannot add any additional methods into it.
So as can be seen, when an object of Activity is created, it has objects of type Address in different areas. Activity class has an object of class Address, and also within man, woman, manager and appointee there are lists of objects belonging to class Address. Is there any way to get all these 'Address' objects from the Activity class using stream function or Apache commonUtils function. Otherwise, I need to traverse through the entire Activity object and get the objects of Address.

答案1

得分: 1

Activity activity = ....;

List<Address> all = Stream.of(
                      activity.getMan().getAddresses(),
                      activity.getWoman().getAddresses(),
                      Arrays.asList(activity.getAddress()))
                .flatMap(List::stream)
                .collect(Collectors.toList());

你可以使用 Stream::of 来将所需的部分连接在一起。首先获取男性的地址,然后是女性的地址,最后是来自 activity 的地址(最后一个必须使用 Arrays::asList 进行封装)。

这将生成一个 Stream<List<Address>>,需要进一步使用 flatMapped 并最终收集到一个 List 中,例如。

当然,你也可以使用传统的方法,创建一个新的 List,分别调用两次 addAll 和一次 add

英文:
Activity activity = ....

List&lt;Address&gt; all = Stream.of(
                  activity.getMan().getAddresses(),
                  activity.getWoman().getAddresses(),
                  Arrays.asList(activity.getAddress()))
            .flatMap(List::stream)
            .collect(Collectors.toList());

You could use Stream::of that will concatenate the needed pieces together. So first get the man addresses, then women address, then the address itself from the activity (the last one has to be wrapped into an Arrays::asList).

This will produce a Stream&lt;List&lt;Address&gt;&gt;, that further needs to be flatMapped and ultimately collected to a List, for example.

That would be using a Stream approach, though simply creating a new List and calling addAll twice and add once, is still an option.

答案2

得分: 1

你可以在Activity类中创建一个方法,该方法创建一个包含所有Address的列表。这是一种非常简单的方法,但易于阅读并避免了使用流操作:

List<Address> getAllAddresses() {
  List<Address> addr = new LinkedList<>();
  addr.add(address);
  addr.addAll(man.address);
  addr.addAll(woman.address);
  return addr;
}

这还允许轻松添加空值检查,比如if(man.address==null)

英文:

You can create a method in the Activity class that creates a list of all Addresses. This a very simple approach, but it is easily readable and avoids streams:

List&lt;Address&gt; getAllAdresses() {
  List&lt;Address&gt; addr = new LinkedList&lt;&gt;();
  addr.add(address);
  addr.addAll(man.address);
  addr.addAll(woman.address);
  return addr;
}

this also allows easy additions of null checks like if(man.address==null).

huangapple
  • 本文由 发表于 2020年7月25日 00:48:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63078000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定