How to remove a field from each object of a list of object?
I have a class FilterEvent with following attributes
filterId
filterValue
filterType
Suppose i have a list of FilterEvent called filterEvents.
I need to remove filterId from each of the object of the list .
I tried in following way
Set<FilterEvent> filterEvents = preparation.getFilterEvents();
filterEvents.stream().map(f->f.setFilterId(null))
to set filterId field set to null . This will not work since my filterId field is of type int .
I need either filterId set to null or need to remove the field itself.
java spring
add a comment |
I have a class FilterEvent with following attributes
filterId
filterValue
filterType
Suppose i have a list of FilterEvent called filterEvents.
I need to remove filterId from each of the object of the list .
I tried in following way
Set<FilterEvent> filterEvents = preparation.getFilterEvents();
filterEvents.stream().map(f->f.setFilterId(null))
to set filterId field set to null . This will not work since my filterId field is of type int .
I need either filterId set to null or need to remove the field itself.
java spring
1
Java is not Python. You can't do that. You can set a flag value, though, as you were trying to do with null. Perhaps -1?
– Mad Physicist
Nov 14 '18 at 12:48
2
You simply can't do this. The fields in an object cannot be removed and you can't set a field of typeinttonull. You need to redefine it asInteger.
– Henry
Nov 14 '18 at 12:49
You can change type offilterIdtoIntegerinstead ofint.
– Jignesh M. Khatri
Nov 14 '18 at 12:49
add a comment |
I have a class FilterEvent with following attributes
filterId
filterValue
filterType
Suppose i have a list of FilterEvent called filterEvents.
I need to remove filterId from each of the object of the list .
I tried in following way
Set<FilterEvent> filterEvents = preparation.getFilterEvents();
filterEvents.stream().map(f->f.setFilterId(null))
to set filterId field set to null . This will not work since my filterId field is of type int .
I need either filterId set to null or need to remove the field itself.
java spring
I have a class FilterEvent with following attributes
filterId
filterValue
filterType
Suppose i have a list of FilterEvent called filterEvents.
I need to remove filterId from each of the object of the list .
I tried in following way
Set<FilterEvent> filterEvents = preparation.getFilterEvents();
filterEvents.stream().map(f->f.setFilterId(null))
to set filterId field set to null . This will not work since my filterId field is of type int .
I need either filterId set to null or need to remove the field itself.
java spring
java spring
asked Nov 14 '18 at 12:46
adarshadarsh
618
618
1
Java is not Python. You can't do that. You can set a flag value, though, as you were trying to do with null. Perhaps -1?
– Mad Physicist
Nov 14 '18 at 12:48
2
You simply can't do this. The fields in an object cannot be removed and you can't set a field of typeinttonull. You need to redefine it asInteger.
– Henry
Nov 14 '18 at 12:49
You can change type offilterIdtoIntegerinstead ofint.
– Jignesh M. Khatri
Nov 14 '18 at 12:49
add a comment |
1
Java is not Python. You can't do that. You can set a flag value, though, as you were trying to do with null. Perhaps -1?
– Mad Physicist
Nov 14 '18 at 12:48
2
You simply can't do this. The fields in an object cannot be removed and you can't set a field of typeinttonull. You need to redefine it asInteger.
– Henry
Nov 14 '18 at 12:49
You can change type offilterIdtoIntegerinstead ofint.
– Jignesh M. Khatri
Nov 14 '18 at 12:49
1
1
Java is not Python. You can't do that. You can set a flag value, though, as you were trying to do with null. Perhaps -1?
– Mad Physicist
Nov 14 '18 at 12:48
Java is not Python. You can't do that. You can set a flag value, though, as you were trying to do with null. Perhaps -1?
– Mad Physicist
Nov 14 '18 at 12:48
2
2
You simply can't do this. The fields in an object cannot be removed and you can't set a field of type
int to null. You need to redefine it as Integer.– Henry
Nov 14 '18 at 12:49
You simply can't do this. The fields in an object cannot be removed and you can't set a field of type
int to null. You need to redefine it as Integer.– Henry
Nov 14 '18 at 12:49
You can change type of
filterId to Integer instead of int.– Jignesh M. Khatri
Nov 14 '18 at 12:49
You can change type of
filterId to Integer instead of int.– Jignesh M. Khatri
Nov 14 '18 at 12:49
add a comment |
4 Answers
4
active
oldest
votes
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?).
okey . very helpful
– adarsh
Nov 14 '18 at 13:06
add a comment |
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.
okey . now understood. It is working
– adarsh
Nov 14 '18 at 13:07
add a comment |
Java constraint is that primitive integer can't be null.
Your problem does not lie in your wrong stream mapping. It is in the int property type of your FilterEvent class. Java constraint is that primitive types (int, long, boolean ...) can't be null.
You have 2 options:
- Either you change the filterId property of FilterEvent class to Integer type which as object can be referenced/set to null.
- Or set the 0 instead of null in f.setFilterId(null).
btw. You should collect the stream or use forEach.
add a comment |
You can't remove a field from an object. The FilterEvent class represents an object that has those three fields. Therefore an object without a filterId field is not a FilterEvent object. You could make another class that represents a cut-down version of FilterEvent; let's call it FilterEventValueAndType. Then, with the relevant constructors and getters, you could do this:
Set<FilterEvent> filterEvents = preparation.getFilterEvents();
Set<FilterEventValueAndType> fevat = filterEvents.stream()
.map(f->new FilterEventValueAndType(f))
.collect(toSet());
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300572%2fhow-to-remove-a-field-from-each-object-of-a-list-of-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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?).
okey . very helpful
– adarsh
Nov 14 '18 at 13:06
add a comment |
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?).
okey . very helpful
– adarsh
Nov 14 '18 at 13:06
add a comment |
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?).
answered Nov 14 '18 at 12:48
EranEran
284k37462550
284k37462550
okey . very helpful
– adarsh
Nov 14 '18 at 13:06
add a comment |
okey . very helpful
– adarsh
Nov 14 '18 at 13:06
okey . very helpful
– adarsh
Nov 14 '18 at 13:06
okey . very helpful
– adarsh
Nov 14 '18 at 13:06
add a comment |
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.
okey . now understood. It is working
– adarsh
Nov 14 '18 at 13:07
add a comment |
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.
okey . now understood. It is working
– adarsh
Nov 14 '18 at 13:07
add a comment |
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.
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.
answered Nov 14 '18 at 12:49
Khalid ShahKhalid Shah
1,3601820
1,3601820
okey . now understood. It is working
– adarsh
Nov 14 '18 at 13:07
add a comment |
okey . now understood. It is working
– adarsh
Nov 14 '18 at 13:07
okey . now understood. It is working
– adarsh
Nov 14 '18 at 13:07
okey . now understood. It is working
– adarsh
Nov 14 '18 at 13:07
add a comment |
Java constraint is that primitive integer can't be null.
Your problem does not lie in your wrong stream mapping. It is in the int property type of your FilterEvent class. Java constraint is that primitive types (int, long, boolean ...) can't be null.
You have 2 options:
- Either you change the filterId property of FilterEvent class to Integer type which as object can be referenced/set to null.
- Or set the 0 instead of null in f.setFilterId(null).
btw. You should collect the stream or use forEach.
add a comment |
Java constraint is that primitive integer can't be null.
Your problem does not lie in your wrong stream mapping. It is in the int property type of your FilterEvent class. Java constraint is that primitive types (int, long, boolean ...) can't be null.
You have 2 options:
- Either you change the filterId property of FilterEvent class to Integer type which as object can be referenced/set to null.
- Or set the 0 instead of null in f.setFilterId(null).
btw. You should collect the stream or use forEach.
add a comment |
Java constraint is that primitive integer can't be null.
Your problem does not lie in your wrong stream mapping. It is in the int property type of your FilterEvent class. Java constraint is that primitive types (int, long, boolean ...) can't be null.
You have 2 options:
- Either you change the filterId property of FilterEvent class to Integer type which as object can be referenced/set to null.
- Or set the 0 instead of null in f.setFilterId(null).
btw. You should collect the stream or use forEach.
Java constraint is that primitive integer can't be null.
Your problem does not lie in your wrong stream mapping. It is in the int property type of your FilterEvent class. Java constraint is that primitive types (int, long, boolean ...) can't be null.
You have 2 options:
- Either you change the filterId property of FilterEvent class to Integer type which as object can be referenced/set to null.
- Or set the 0 instead of null in f.setFilterId(null).
btw. You should collect the stream or use forEach.
edited Nov 14 '18 at 13:01
answered Nov 14 '18 at 12:53
Andrej BudayAndrej Buday
17210
17210
add a comment |
add a comment |
You can't remove a field from an object. The FilterEvent class represents an object that has those three fields. Therefore an object without a filterId field is not a FilterEvent object. You could make another class that represents a cut-down version of FilterEvent; let's call it FilterEventValueAndType. Then, with the relevant constructors and getters, you could do this:
Set<FilterEvent> filterEvents = preparation.getFilterEvents();
Set<FilterEventValueAndType> fevat = filterEvents.stream()
.map(f->new FilterEventValueAndType(f))
.collect(toSet());
add a comment |
You can't remove a field from an object. The FilterEvent class represents an object that has those three fields. Therefore an object without a filterId field is not a FilterEvent object. You could make another class that represents a cut-down version of FilterEvent; let's call it FilterEventValueAndType. Then, with the relevant constructors and getters, you could do this:
Set<FilterEvent> filterEvents = preparation.getFilterEvents();
Set<FilterEventValueAndType> fevat = filterEvents.stream()
.map(f->new FilterEventValueAndType(f))
.collect(toSet());
add a comment |
You can't remove a field from an object. The FilterEvent class represents an object that has those three fields. Therefore an object without a filterId field is not a FilterEvent object. You could make another class that represents a cut-down version of FilterEvent; let's call it FilterEventValueAndType. Then, with the relevant constructors and getters, you could do this:
Set<FilterEvent> filterEvents = preparation.getFilterEvents();
Set<FilterEventValueAndType> fevat = filterEvents.stream()
.map(f->new FilterEventValueAndType(f))
.collect(toSet());
You can't remove a field from an object. The FilterEvent class represents an object that has those three fields. Therefore an object without a filterId field is not a FilterEvent object. You could make another class that represents a cut-down version of FilterEvent; let's call it FilterEventValueAndType. Then, with the relevant constructors and getters, you could do this:
Set<FilterEvent> filterEvents = preparation.getFilterEvents();
Set<FilterEventValueAndType> fevat = filterEvents.stream()
.map(f->new FilterEventValueAndType(f))
.collect(toSet());
answered Nov 14 '18 at 13:30
DodgyCodeExceptionDodgyCodeException
3,5031424
3,5031424
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300572%2fhow-to-remove-a-field-from-each-object-of-a-list-of-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Java is not Python. You can't do that. You can set a flag value, though, as you were trying to do with null. Perhaps -1?
– Mad Physicist
Nov 14 '18 at 12:48
2
You simply can't do this. The fields in an object cannot be removed and you can't set a field of type
inttonull. You need to redefine it asInteger.– Henry
Nov 14 '18 at 12:49
You can change type of
filterIdtoIntegerinstead ofint.– Jignesh M. Khatri
Nov 14 '18 at 12:49