map won't work, since it's an intermediate operation, so it won't be executed unless it is followed by some terminal operation.
Use forEach:
Set<FilterEvent> filterEvents = preparation.getFilterEvents();
filterEvents.forEach(f->f.setFilterId(null));
However, if setFilterId expects an int, you can't pass null. You'll have to set it to some other value (0?).
map won't work, since it's an intermediate operation, so it won't be executed unless it is followed by some terminal operation.
Use forEach:
Set<FilterEvent> filterEvents = preparation.getFilterEvents();
filterEvents.forEach(f->f.setFilterId(null));
However, if setFilterId expects an int, you can't pass null. You'll have to set it to some other value (0?).
int is a primitive type so if you want to set null then change its data type from int to Integer and
filterEvents.stream().forEach(f->f.setFilterId(null));
or if you don't want to change the data type then set its default value i.e 0 or -1.
As it mentioned in comments above, you cant remove fields of compiled class at runtime. Assuming you have to exclude some field from generated json, there I see two options:
- Create a class with fields you want to be present in resulting json, copy required values from original object to a new created. This approach is called view model and allows you to decorate some object's data, hiding sensitive data from being exposed.
- Depending on implementation of your serializer there may be annotations to exclude fields.
@JsonIgnoremay be placed on getter method, if you are using Jackson (default in spring boot). Second aproach requires significant less code, but the first one is more flexible.
Try @JsonIgnore to ignore properties from serialization and de-serialization.
Here is the link to the docs
You could implement an annotiation. Let's call it @DontPersist. Use it to mark fields which should not get persisted. In Utility.query() you can check for the annotation with reflection.
As your Model class does not implement anything (it could be an interface, but that's another topic), you can extend it creating a class with less attributes when necessary (an anonymous class will do the job).
Anyway, I think you should refactor your code: why not using a List to store fields? It's easier and it does not need reflection.
I'll use something like:
public class Model extends ArrayList{
public Model(String name) { tableName=name;}
private String tableName;
public String getTable() {return tableName}
}
And then you can iterate over the Array to obtain the field names.
put all your instances of your object in a collection, and then you can delete it from the collection.
List<YourObject> list = new ArrayList<YourObject>();
YourObject obj1 = new YourObject("abc");
list.add(obj1);
YourObject obj2 = new YourObject("xyz");
list.add(obj2);
now both your objects are inside a list . later you can use the remove method an remove them.
list.remove(obj1);
and just a pointer, its a bad practice to name your class as Object as all java classes extend from java.lang.Object.
public class Object {
private Credentials credentials;
private int PageSize;
private int PageStart;
private int DefaultFilterId;
public Object(Credentials credentials, int PageSize, int PageStart, int DefaultFilterId) {
this.credentials = credentials;
this.PageSize = PageSize;
this.PageStart = PageStart;
this.DefaultFilterId = DefaultFilterId;
}
// do that for the properties you want to be able to modify
public void setCredentials(Credentials newCredentials) {
this.credentials = newCredentials;
}
}
And you use that:
object.setCredentials(yourNewCredentials)
Also, you shouldn't name your object "Object", it's the base class for all the classes in Java.