gpt4 book ai didi

java - 如何删除ArrayList中对象的属性

转载 作者:行者123 更新时间:2023-11-30 02:07:57 26 4
gpt4 key购买 nike

我有一个返回大量数据的端点,我想删除其中的一部分。

例如:

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;
}

Controller

@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< 中“处理”属性.

我应该只对每个循环使用还是迭代器吗?我还应该将属性设置为 null 或其他方法吗?

最佳答案

我怀疑是否可以在不迭代的情况下更改该属性。不过您可以尝试 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]]

}

学生类(class)原样

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 + "]";
}
}

关于java - 如何删除ArrayList中对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50965918/

26 4 0