Concatenating two int[]How can I concatenate two arrays in Java?Fastest way to determine if an integer's...

Single-row INSERT...SELECT much slower than separate SELECT

Are all power cords made equal?

The No-Straight Maze

What to do with threats of blacklisting?

How do I avoid the "chosen hero" feeling?

Buying a "Used" Router

How can I give a Ranger advantage on a check due to Favored Enemy without spoiling the story for the player?

How to politely refuse in-office gym instructor for steroids and protein

What is an efficient way to digitize a family photo collection?

Why did Mr. Elliot have to decide whose boots were thickest in "Persuasion"?

If angels and devils are the same species, why would their mortal offspring appear physically different?

To be or not to be - Optional arguments inside definition of macro

How long has this character been impersonating a Starfleet Officer?

Boss asked me to sign a resignation paper without a date on it along with my new contract

Is there any danger of my neighbor having my wife's signature?

Count repetitions of an array

How do I narratively explain how in-game circumstances do not mechanically allow a PC to instantly kill an NPC?

Why didn't Tom Riddle take the presence of Fawkes and the Sorting Hat as more of a threat?

How vim overwrites readonly mode?

Concatenating two int[]

How to extract specific values/fields from the text file?

Is `Object` a function in javascript?

Why are samba client and NFS client used differently?

Other than edits for international editions, did Harry Potter and the Philosopher's Stone receive errata?



Concatenating two int[]


How can I concatenate two arrays in Java?Fastest way to determine if an integer's square root is an integerHow can I create a two dimensional array in JavaScript?StringBuilder vs String concatenation in toString() in JavaHow to merge two arrays in JavaScript and de-duplicate itemsHow do I convert a String to an int in Java?Why is subtracting these two times (in 1927) giving a strange result?int to String while String concatenation in javaconvert Object[] from stream to byte[]Java8 Integer Stream Vs IntStream













6















There are easy solutions for concatenating two String[] or Integer[] in java by Streams. Since int[] is frequently used. Is there any straightforward way for concatenating two int[]?



Here is my thought:



int[] c = {1, 34};
int[] d = {3, 1, 5};
Integer[] cc = IntStream.of(c).boxed().toArray(Integer[]::new);
Integer[] dd = Arrays.stream(d).boxed().toArray(Integer[]::new);
int[] m = Stream.concat(Stream.of(cc), Stream.of(dd)).mapToInt(Integer::intValue).toArray();
System.out.println(Arrays.toString(m));

>>
[1, 34, 3, 1, 5]


It works, but it actually converts int[] to Integer[], then converts Integer[] back to int[] again.










share|improve this question





























    6















    There are easy solutions for concatenating two String[] or Integer[] in java by Streams. Since int[] is frequently used. Is there any straightforward way for concatenating two int[]?



    Here is my thought:



    int[] c = {1, 34};
    int[] d = {3, 1, 5};
    Integer[] cc = IntStream.of(c).boxed().toArray(Integer[]::new);
    Integer[] dd = Arrays.stream(d).boxed().toArray(Integer[]::new);
    int[] m = Stream.concat(Stream.of(cc), Stream.of(dd)).mapToInt(Integer::intValue).toArray();
    System.out.println(Arrays.toString(m));

    >>
    [1, 34, 3, 1, 5]


    It works, but it actually converts int[] to Integer[], then converts Integer[] back to int[] again.










    share|improve this question



























      6












      6








      6








      There are easy solutions for concatenating two String[] or Integer[] in java by Streams. Since int[] is frequently used. Is there any straightforward way for concatenating two int[]?



      Here is my thought:



      int[] c = {1, 34};
      int[] d = {3, 1, 5};
      Integer[] cc = IntStream.of(c).boxed().toArray(Integer[]::new);
      Integer[] dd = Arrays.stream(d).boxed().toArray(Integer[]::new);
      int[] m = Stream.concat(Stream.of(cc), Stream.of(dd)).mapToInt(Integer::intValue).toArray();
      System.out.println(Arrays.toString(m));

      >>
      [1, 34, 3, 1, 5]


      It works, but it actually converts int[] to Integer[], then converts Integer[] back to int[] again.










      share|improve this question
















      There are easy solutions for concatenating two String[] or Integer[] in java by Streams. Since int[] is frequently used. Is there any straightforward way for concatenating two int[]?



      Here is my thought:



      int[] c = {1, 34};
      int[] d = {3, 1, 5};
      Integer[] cc = IntStream.of(c).boxed().toArray(Integer[]::new);
      Integer[] dd = Arrays.stream(d).boxed().toArray(Integer[]::new);
      int[] m = Stream.concat(Stream.of(cc), Stream.of(dd)).mapToInt(Integer::intValue).toArray();
      System.out.println(Arrays.toString(m));

      >>
      [1, 34, 3, 1, 5]


      It works, but it actually converts int[] to Integer[], then converts Integer[] back to int[] again.







      java arrays lambda java-8 java-stream






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago









      nullpointer

      42.9k10101196




      42.9k10101196










      asked 3 hours ago









      Simon ZhangSimon Zhang

      343




      343
























          2 Answers
          2






          active

          oldest

          votes


















          5














          You can use IntStream.concat in concert with Arrays.stream to get this thing done without any auto-boxing or unboxing. Here's how it looks.



          int[] result = IntStream.concat(Arrays.stream(c), Arrays.stream(d)).toArray();


          Note that Arrays.stream(c) returns an IntStream, which is then concatenated with the other IntStream before collected into an array.



          Here's the output.




          [1, 34, 3, 1, 5]







          share|improve this answer

































            5














            You can simply concatenate primitive(int) streams as:



            int[] m = IntStream.concat(IntStream.of(c), IntStream.of(d)).toArray();





            share|improve this answer
























            • Just reads better in my opinion, though notably, the IntStream.of under the hood makes use of the Arrays.stream itself.

              – nullpointer
              2 hours ago













            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%2f54864223%2fconcatenating-two-int%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









            5














            You can use IntStream.concat in concert with Arrays.stream to get this thing done without any auto-boxing or unboxing. Here's how it looks.



            int[] result = IntStream.concat(Arrays.stream(c), Arrays.stream(d)).toArray();


            Note that Arrays.stream(c) returns an IntStream, which is then concatenated with the other IntStream before collected into an array.



            Here's the output.




            [1, 34, 3, 1, 5]







            share|improve this answer






























              5














              You can use IntStream.concat in concert with Arrays.stream to get this thing done without any auto-boxing or unboxing. Here's how it looks.



              int[] result = IntStream.concat(Arrays.stream(c), Arrays.stream(d)).toArray();


              Note that Arrays.stream(c) returns an IntStream, which is then concatenated with the other IntStream before collected into an array.



              Here's the output.




              [1, 34, 3, 1, 5]







              share|improve this answer




























                5












                5








                5







                You can use IntStream.concat in concert with Arrays.stream to get this thing done without any auto-boxing or unboxing. Here's how it looks.



                int[] result = IntStream.concat(Arrays.stream(c), Arrays.stream(d)).toArray();


                Note that Arrays.stream(c) returns an IntStream, which is then concatenated with the other IntStream before collected into an array.



                Here's the output.




                [1, 34, 3, 1, 5]







                share|improve this answer















                You can use IntStream.concat in concert with Arrays.stream to get this thing done without any auto-boxing or unboxing. Here's how it looks.



                int[] result = IntStream.concat(Arrays.stream(c), Arrays.stream(d)).toArray();


                Note that Arrays.stream(c) returns an IntStream, which is then concatenated with the other IntStream before collected into an array.



                Here's the output.




                [1, 34, 3, 1, 5]








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 2 hours ago

























                answered 3 hours ago









                Ravindra RanwalaRavindra Ranwala

                10.1k31937




                10.1k31937

























                    5














                    You can simply concatenate primitive(int) streams as:



                    int[] m = IntStream.concat(IntStream.of(c), IntStream.of(d)).toArray();





                    share|improve this answer
























                    • Just reads better in my opinion, though notably, the IntStream.of under the hood makes use of the Arrays.stream itself.

                      – nullpointer
                      2 hours ago


















                    5














                    You can simply concatenate primitive(int) streams as:



                    int[] m = IntStream.concat(IntStream.of(c), IntStream.of(d)).toArray();





                    share|improve this answer
























                    • Just reads better in my opinion, though notably, the IntStream.of under the hood makes use of the Arrays.stream itself.

                      – nullpointer
                      2 hours ago
















                    5












                    5








                    5







                    You can simply concatenate primitive(int) streams as:



                    int[] m = IntStream.concat(IntStream.of(c), IntStream.of(d)).toArray();





                    share|improve this answer













                    You can simply concatenate primitive(int) streams as:



                    int[] m = IntStream.concat(IntStream.of(c), IntStream.of(d)).toArray();






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 2 hours ago









                    nullpointernullpointer

                    42.9k10101196




                    42.9k10101196













                    • Just reads better in my opinion, though notably, the IntStream.of under the hood makes use of the Arrays.stream itself.

                      – nullpointer
                      2 hours ago





















                    • Just reads better in my opinion, though notably, the IntStream.of under the hood makes use of the Arrays.stream itself.

                      – nullpointer
                      2 hours ago



















                    Just reads better in my opinion, though notably, the IntStream.of under the hood makes use of the Arrays.stream itself.

                    – nullpointer
                    2 hours ago







                    Just reads better in my opinion, though notably, the IntStream.of under the hood makes use of the Arrays.stream itself.

                    – nullpointer
                    2 hours ago




















                    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%2f54864223%2fconcatenating-two-int%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

                    Szabolcs (Ungheria) Altri progetti | Menu di navigazione48°10′14.56″N 21°29′33.14″E /...

                    Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

                    How to make inet_server_addr() return localhost in spite of ::1/128RETURN NEXT in Postgres FunctionConnect to...