英文:
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类的对象,而在man、woman、manager和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<Address> address;
List<Appointee> appointees;
}
class Woman{
String name;
List<Phone> phones;
List<Address> address;
Manager manager;
}
class Manager{
String position;
Adddress address;
}
class Appointee{
String type;
List<Address> 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<Address> 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<List<Address>>
, 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 Address
es. This a very simple approach, but it is easily readable and avoids streams:
List<Address> getAllAdresses() {
List<Address> addr = new LinkedList<>();
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)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论