我有一个返回大数据的端点,我想删除其中的一部分。
例如:
A级
public class A{
private String id;
private Date createOn;
private String processed;
}
B类
public class B extends MongoDBObject{
private String id;
private Date createOn;
private String processed;
}
控制器
@RestController
@RequestMapping("/v1/read")
public class ReadController{
@Autowired
private StatementBundleService bundleService;
@CrossOrigin
@GetMapping(value = "/statementBundles")
public List<A> listStatements() {
List<A> result = new ArrayList<A>();
List<B> bundles = bundleService.getAll();
for(B bundle: bundles) {
result.add(new A(bundle));
}
return result;
}
我试图找出最好的方法是返回A
和A
类都没有“处理过”属性的B
列表。
我应该只使用for each
循环还是iterator
?还应该将属性设置为null
还是其他方法?
答案 0 :(得分:2)
我怀疑是否可以在不迭代的情况下更改属性。 虽然您可以尝试java8来进行快速简单的输出。看看里面。
public class Java8 {
public static void main(String[] args) {
List<Student> myList = new ArrayList<Student>();
myList.add(new Student(1, "John", "John is a good Student"));
myList.add(new Student(1, "Paul", "Paul is a good Player"));
myList.add(new Student(1, "Tom", "Paul is a good Teacher"));
System.out.println(myList);//old list
myList = myList.stream().peek(obj -> obj.setBiography(null)).collect(Collectors.toList());
System.out.println(myList);//new list
}
/*Output*/
//[Student [id=1, Name=John, biography=John is a good Student], Student [id=1, Name=Paul, biography=Paul is a good Player], Student [id=1, Name=Tom, biography=Paul is a good Teacher]]
//[Student [id=1, Name=John, biography=null], Student [id=1, Name=Paul, biography=null], Student [id=1, Name=Tom, biography=null]]
}
按原样的学生班级
public class Student{
private int id;
private String Name;
private String biography;
public Student(int id, String name, String biography) {
super();
this.id = id;
Name = name;
this.biography = biography;
}
public int getId() {
return id;
}
public String getBiography() {
return biography;
}
public void setBiography(String biography) {
this.biography = biography;
}
@Override
public String toString() {
return "Student [id=" + id + ", Name=" + Name + ", biography=" + biography + "]";
}
}