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
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
add a comment |
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
add a comment |
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
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
java arrays lambda java-8 java-stream
edited 2 hours ago
nullpointer
42.9k10101196
42.9k10101196
asked 3 hours ago
Simon ZhangSimon Zhang
343
343
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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]
add a comment |
You can simply concatenate primitive(int
) streams as:
int[] m = IntStream.concat(IntStream.of(c), IntStream.of(d)).toArray();
Just reads better in my opinion, though notably, theIntStream.of
under the hood makes use of theArrays.stream
itself.
– nullpointer
2 hours ago
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%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
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]
add a comment |
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]
add a comment |
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]
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]
edited 2 hours ago
answered 3 hours ago
Ravindra RanwalaRavindra Ranwala
10.1k31937
10.1k31937
add a comment |
add a comment |
You can simply concatenate primitive(int
) streams as:
int[] m = IntStream.concat(IntStream.of(c), IntStream.of(d)).toArray();
Just reads better in my opinion, though notably, theIntStream.of
under the hood makes use of theArrays.stream
itself.
– nullpointer
2 hours ago
add a comment |
You can simply concatenate primitive(int
) streams as:
int[] m = IntStream.concat(IntStream.of(c), IntStream.of(d)).toArray();
Just reads better in my opinion, though notably, theIntStream.of
under the hood makes use of theArrays.stream
itself.
– nullpointer
2 hours ago
add a comment |
You can simply concatenate primitive(int
) streams as:
int[] m = IntStream.concat(IntStream.of(c), IntStream.of(d)).toArray();
You can simply concatenate primitive(int
) streams as:
int[] m = IntStream.concat(IntStream.of(c), IntStream.of(d)).toArray();
answered 2 hours ago
nullpointernullpointer
42.9k10101196
42.9k10101196
Just reads better in my opinion, though notably, theIntStream.of
under the hood makes use of theArrays.stream
itself.
– nullpointer
2 hours ago
add a comment |
Just reads better in my opinion, though notably, theIntStream.of
under the hood makes use of theArrays.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
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%2f54864223%2fconcatenating-two-int%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