public class StartObject{
private Something something;
private Set<ObjectThatMatters> objectThatMattersSet;
}
public class Something{
private Set<SomeObject> someObjecSet;
}
public class SomeObject {
private AnotherObject anotherObjectSet;
}
public class AnotherObject{
private Set<ObjectThatMatters> objectThatMattersSet;
}
public class ObjectThatMatters{
private Long id;
}
private void someMethod(StartObject startObject) {
Map<Long, ObjectThatMatters> objectThatMattersMap = StartObject.getSomething()
.getSomeObject.stream()
.map(getSomeObject::getAnotherObject)
.flatMap(anotherObject-> anotherObject.getObjectThatMattersSet().stream())
.collect(Collectors.toMap(ObjectThatMatters -> ObjectThatMatters.getId(), Function.identity()));
Set<ObjectThatMatters > dbObjectThatMatters = new HashSet<>();
try {
dbObjectThatMatters.addAll( tartObject.getObjectThatMatters().stream().map(objectThatMatters-> objectThatMattersMap .get(objectThatMatters.getId())).collect(Collectors.toSet()));
} catch (NullPointerException e) {
throw new someCustomException();
}
startObject.setObjectThatMattersSet(dbObjectThatMatters);给一个包含一组ObjectThatMatters的StartObject
和--包含已被所有有效ObjectThatMatters填充的数据库结构的东西。
当时,我希望将ObjectThatMatters的StartObject集交换为仅存在于某物范围内的有效的相应db对象
然后我比较了StartObject上的ObjectThatMatters集
和将它们替换为对象内的有效ObjectThatMatters。
和如果某些ObjectThatMatters没有有效的ObjectThatMatters,则抛出一个someCustomException
这个someMethod看起来很可怕,我怎样才能使它更易读?
已经尝试过将that更改为一个可选的,但这实际上没有帮助。
由于性能原因,使用Map而不是使用List.contains的列表,这是个好主意吗?ObjectThatMatters的总数通常是500个。
我不允许更改其他类的结构,我只向您展示影响此方法的字段,而不是每个字段,因为它们是非常丰富的对象。
发布于 2016-11-01 15:35:29
您根本不需要一个映射步骤。第一个操作产生一个Map,首先可以用于生成所需的Set。由于可能有比您感兴趣的对象更多的对象,所以可以执行筛选操作。
因此,首先,将所需对象的ID收集到一个集合中,然后收集相应的db对象,由ID的Set进行过滤。您可以通过比较结果的Set大小和ID Set的大小来验证是否找到了所有的ID。
private void someMethod(StartObject startObject) {
Set<Long> id = startObject.getObjectThatMatters().stream()
.map(ObjectThatMatters::getId).collect(Collectors.toSet());
HashSet<ObjectThatMatters> objectThatMattersSet =
startObject.getSomething().getSomeObject().stream()
.flatMap(so -> so.getAnotherObject().getObjectThatMattersSet().stream())
.filter(obj -> id.contains(obj.getId()))
.collect(Collectors.toCollection(HashSet::new));
if(objectThatMattersSet.size() != id.size())
throw new SomeCustomException();
startObject.setObjectThatMattersSet(objectThatMattersSet);
}这段代码生成一个HashSet;如果这不是一个要求,您可以只使用Collectors.toSet()来获得一个任意的Set实现。
甚至很容易发现哪些It丢失了:
private void someMethod(StartObject startObject) {
Set<Long> id = startObject.getObjectThatMatters().stream()
.map(ObjectThatMatters::getId)
.collect(Collectors.toCollection(HashSet::new));// ensure mutable Set
HashSet<ObjectThatMatters> objectThatMattersSet =
startObject.getSomething().getSomeObject().stream()
.flatMap(so -> so.getAnotherObject().getObjectThatMattersSet().stream())
.filter(obj -> id.contains(obj.getId()))
.collect(Collectors.toCollection(HashSet::new));
if(objectThatMattersSet.size() != id.size()) {
objectThatMattersSet.stream().map(ObjectThatMatters::getId).forEach(id::remove);
throw new SomeCustomException("The following IDs were not found: "+id);
}
startObject.setObjectThatMattersSet(objectThatMattersSet);
}https://stackoverflow.com/questions/40362393
复制相似问题