How to remove a field from each object of a list of object?












2















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.










share|improve this question


















  • 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 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
















2















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.










share|improve this question


















  • 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 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














2












2








2








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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 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








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












4 Answers
4






active

oldest

votes


















3














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?).






share|improve this answer
























  • okey . very helpful

    – adarsh
    Nov 14 '18 at 13:06



















2














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.






share|improve this answer
























  • okey . now understood. It is working

    – adarsh
    Nov 14 '18 at 13:07



















2














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.






share|improve this answer

































    0














    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());





    share|improve this answer























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


      }
      });














      draft saved

      draft discarded


















      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









      3














      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?).






      share|improve this answer
























      • okey . very helpful

        – adarsh
        Nov 14 '18 at 13:06
















      3














      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?).






      share|improve this answer
























      • okey . very helpful

        – adarsh
        Nov 14 '18 at 13:06














      3












      3








      3







      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?).






      share|improve this answer













      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?).







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 14 '18 at 12:48









      EranEran

      284k37462550




      284k37462550













      • 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





      okey . very helpful

      – adarsh
      Nov 14 '18 at 13:06













      2














      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.






      share|improve this answer
























      • okey . now understood. It is working

        – adarsh
        Nov 14 '18 at 13:07
















      2














      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.






      share|improve this answer
























      • okey . now understood. It is working

        – adarsh
        Nov 14 '18 at 13:07














      2












      2








      2







      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.






      share|improve this answer













      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.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      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



















      • 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











      2














      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.






      share|improve this answer






























        2














        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.






        share|improve this answer




























          2












          2








          2







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 13:01

























          answered Nov 14 '18 at 12:53









          Andrej BudayAndrej Buday

          17210




          17210























              0














              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());





              share|improve this answer




























                0














                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());





                share|improve this answer


























                  0












                  0








                  0







                  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());





                  share|improve this answer













                  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());






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 13:30









                  DodgyCodeExceptionDodgyCodeException

                  3,5031424




                  3,5031424






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      List item for chat from Array inside array React Native

                      Thiostrepton

                      Caerphilly