How to check if an array contains a partial value once?









up vote
-1
down vote

favorite












I have an array,



arr = [name1 class age, name2 class age, name3 class age, name1 class age, name2 class age]


I want to check each item and store it in another array



String r = Arrays.stream(arr)
.filter(s -> s.contains("name1"))
.toArray(String::new);


How do I save just the first name1 using the above code?



output: [name1 class age, name1 class age]



expected output: [name1 class age]










share|improve this question



















  • 1




    what's not working here? Post a Minimal, Complete, and Verifiable example
    – Reimeus
    Nov 6 at 20:55










  • @Reimeus Please check the updated post
    – c.r
    Nov 6 at 20:57














up vote
-1
down vote

favorite












I have an array,



arr = [name1 class age, name2 class age, name3 class age, name1 class age, name2 class age]


I want to check each item and store it in another array



String r = Arrays.stream(arr)
.filter(s -> s.contains("name1"))
.toArray(String::new);


How do I save just the first name1 using the above code?



output: [name1 class age, name1 class age]



expected output: [name1 class age]










share|improve this question



















  • 1




    what's not working here? Post a Minimal, Complete, and Verifiable example
    – Reimeus
    Nov 6 at 20:55










  • @Reimeus Please check the updated post
    – c.r
    Nov 6 at 20:57












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have an array,



arr = [name1 class age, name2 class age, name3 class age, name1 class age, name2 class age]


I want to check each item and store it in another array



String r = Arrays.stream(arr)
.filter(s -> s.contains("name1"))
.toArray(String::new);


How do I save just the first name1 using the above code?



output: [name1 class age, name1 class age]



expected output: [name1 class age]










share|improve this question















I have an array,



arr = [name1 class age, name2 class age, name3 class age, name1 class age, name2 class age]


I want to check each item and store it in another array



String r = Arrays.stream(arr)
.filter(s -> s.contains("name1"))
.toArray(String::new);


How do I save just the first name1 using the above code?



output: [name1 class age, name1 class age]



expected output: [name1 class age]







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 22:46









Zabuza

11k52538




11k52538










asked Nov 6 at 20:47









c.r

188




188







  • 1




    what's not working here? Post a Minimal, Complete, and Verifiable example
    – Reimeus
    Nov 6 at 20:55










  • @Reimeus Please check the updated post
    – c.r
    Nov 6 at 20:57












  • 1




    what's not working here? Post a Minimal, Complete, and Verifiable example
    – Reimeus
    Nov 6 at 20:55










  • @Reimeus Please check the updated post
    – c.r
    Nov 6 at 20:57







1




1




what's not working here? Post a Minimal, Complete, and Verifiable example
– Reimeus
Nov 6 at 20:55




what's not working here? Post a Minimal, Complete, and Verifiable example
– Reimeus
Nov 6 at 20:55












@Reimeus Please check the updated post
– c.r
Nov 6 at 20:57




@Reimeus Please check the updated post
– c.r
Nov 6 at 20:57












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










In case you need to retrieve only the first found element:



String r = new String Arrays.stream(arr)
.filter(s -> s.contains("name1"))
.findFirst().orElse("") ;





share|improve this answer





























    up vote
    1
    down vote













    You could map the first match



    String array = 
    Arrays.stream(arr).filter
    (s -> s.contains("name1")).findFirst().map(s -> new String s ).orElse(new String );





    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',
      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%2f53179779%2fhow-to-check-if-an-array-contains-a-partial-value-once%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      In case you need to retrieve only the first found element:



      String r = new String Arrays.stream(arr)
      .filter(s -> s.contains("name1"))
      .findFirst().orElse("") ;





      share|improve this answer


























        up vote
        1
        down vote



        accepted










        In case you need to retrieve only the first found element:



        String r = new String Arrays.stream(arr)
        .filter(s -> s.contains("name1"))
        .findFirst().orElse("") ;





        share|improve this answer
























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          In case you need to retrieve only the first found element:



          String r = new String Arrays.stream(arr)
          .filter(s -> s.contains("name1"))
          .findFirst().orElse("") ;





          share|improve this answer














          In case you need to retrieve only the first found element:



          String r = new String Arrays.stream(arr)
          .filter(s -> s.contains("name1"))
          .findFirst().orElse("") ;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 22:21









          marc_s

          566k12610921245




          566k12610921245










          answered Nov 6 at 21:24









          oleg.cherednik

          4,3132916




          4,3132916






















              up vote
              1
              down vote













              You could map the first match



              String array = 
              Arrays.stream(arr).filter
              (s -> s.contains("name1")).findFirst().map(s -> new String s ).orElse(new String );





              share|improve this answer


























                up vote
                1
                down vote













                You could map the first match



                String array = 
                Arrays.stream(arr).filter
                (s -> s.contains("name1")).findFirst().map(s -> new String s ).orElse(new String );





                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You could map the first match



                  String array = 
                  Arrays.stream(arr).filter
                  (s -> s.contains("name1")).findFirst().map(s -> new String s ).orElse(new String );





                  share|improve this answer














                  You could map the first match



                  String array = 
                  Arrays.stream(arr).filter
                  (s -> s.contains("name1")).findFirst().map(s -> new String s ).orElse(new String );






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 6 at 21:50

























                  answered Nov 6 at 21:00









                  Reimeus

                  140k10155221




                  140k10155221



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53179779%2fhow-to-check-if-an-array-contains-a-partial-value-once%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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto