我有这个锅炉房代码
for(Person p: school.getStudents())
p.setName(null);
for(Person p: school.getTeachers())
p.setName(null);
for(Person p: school.getStrangers())
p.setName(null);
我可以在 View 中“组合”所有这些集合(即避免像通过 addAll()
那样复制/分配内存)并迭代 View 吗?
例如
for(Person p: allOf(school.getStudents(), school.getTeachers(), school.getStrangers())
p.setName(null);
最佳答案
您可以使用Guava的Iterables.concat()
方法。它在传递给它的多个可迭代对象上创建一个 View ,然后您可以迭代生成的Iterable
。
关于java - 如何在Java中创建多个集合中所有元素的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24675766/