如何删除 ArrayList 中对象的属性
How to remove properties of objects in ArrayList
我有一个终点,其中 return 有大量数据,我想删除其中的一部分。
例如:
Class一个
public class A{
private String id;
private Date createOn;
private String processed;
}
Class 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;
}
我想弄清楚 return A
的列表没有 class A
和 "processed" 属性的最佳方法是什么class B
.
我应该只使用 for each
循环还是 iterator
?我还应该将属性设置为 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 + "]";
}
}
我有一个终点,其中 return 有大量数据,我想删除其中的一部分。
例如:
Class一个
public class A{
private String id;
private Date createOn;
private String processed;
}
Class 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;
}
我想弄清楚 return A
的列表没有 class A
和 "processed" 属性的最佳方法是什么class B
.
我应该只使用 for each
循环还是 iterator
?我还应该将属性设置为 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 + "]";
}
}