Deep comparing each element in an array
I have the following 3 files,
A.java:
class A {
private float b;
public A(float b) {
this.b = b;
}
public float getB() {
return b;
}
public String toString() {
return "A(b = " + b + ")";
}
}
C.java:
import java.util.Arrays;
class C {
private A d;
private int i = 0;
public C() {
d = new A[5];
}
public void addB(A b) {
d[i++] = b;
}
public String toString() {
return "C(b = " + Arrays.toString(d) + ")";
}
public void duplicate() {
A temp = Arrays.copyOf(d, d.length);
for (int cur = 0; cur < d.length; cur++) {
if (d[cur] == null) continue;
float currB = d[cur].getB();
for (int nxt = cur + 1; nxt < d.length; nxt++) {
if(d[nxt] == null) continue;
if(currB == d[nxt].getB()) {
temp[i++] = new A(currB * 0.5f);
}
}
}
d = temp;
}
}
D.java:
class D {
public static void main(String args) {
C c = new C();
c.addB(new A(3));
c.addB(new A(5));
c.addB(new A(3));
System.out.println(c.toString()); // C(b = [A(b = 3.0), A(b = 5.0), A(b = 3.0), null, null])
c.duplicate();
System.out.println(c.toString()); // C(b = [A(b = 3.0), A(b = 5.0), A(b = 3.0), A(b = 1.5), null])
}
}
This does what I expected it to do, which is add another item to the array with half the b
if two of the elements have the same returned float from A.getB()
. However, I was trying to implement this using the fancy Java 8 stream methods and lambda functions, like so:
Arrays.stream(d).anyMatch(cur -> {
if (cur == null) return false;
Arrays.stream(d).anyMatch(nxt -> {
if (nxt == null) return false;
System.out.println("Checking " + cur.getB() + " with " + nxt.getB());
return false;
});
return false;
});
And this outputted:
Checking 3.0 with 3.0
Checking 3.0 with 5.0
Checking 3.0 with 3.0
Checking 5.0 with 3.0
Checking 5.0 with 5.0
Checking 5.0 with 3.0
Checking 3.0 with 3.0
Checking 3.0 with 5.0
Checking 3.0 with 3.0
As you can see this follows the O(n²) algorithm which is not what I'm going for. In my original code I was "skipping" elements that I already checked by using the indexes from the outer nested for loop. So my question is, if there is a way to somehow implement this in the nested <Stream>.anyMatch(...)
that I attempted. Or is there a cleaner way of doing this?
java java-8 java-stream
|
show 1 more comment
I have the following 3 files,
A.java:
class A {
private float b;
public A(float b) {
this.b = b;
}
public float getB() {
return b;
}
public String toString() {
return "A(b = " + b + ")";
}
}
C.java:
import java.util.Arrays;
class C {
private A d;
private int i = 0;
public C() {
d = new A[5];
}
public void addB(A b) {
d[i++] = b;
}
public String toString() {
return "C(b = " + Arrays.toString(d) + ")";
}
public void duplicate() {
A temp = Arrays.copyOf(d, d.length);
for (int cur = 0; cur < d.length; cur++) {
if (d[cur] == null) continue;
float currB = d[cur].getB();
for (int nxt = cur + 1; nxt < d.length; nxt++) {
if(d[nxt] == null) continue;
if(currB == d[nxt].getB()) {
temp[i++] = new A(currB * 0.5f);
}
}
}
d = temp;
}
}
D.java:
class D {
public static void main(String args) {
C c = new C();
c.addB(new A(3));
c.addB(new A(5));
c.addB(new A(3));
System.out.println(c.toString()); // C(b = [A(b = 3.0), A(b = 5.0), A(b = 3.0), null, null])
c.duplicate();
System.out.println(c.toString()); // C(b = [A(b = 3.0), A(b = 5.0), A(b = 3.0), A(b = 1.5), null])
}
}
This does what I expected it to do, which is add another item to the array with half the b
if two of the elements have the same returned float from A.getB()
. However, I was trying to implement this using the fancy Java 8 stream methods and lambda functions, like so:
Arrays.stream(d).anyMatch(cur -> {
if (cur == null) return false;
Arrays.stream(d).anyMatch(nxt -> {
if (nxt == null) return false;
System.out.println("Checking " + cur.getB() + " with " + nxt.getB());
return false;
});
return false;
});
And this outputted:
Checking 3.0 with 3.0
Checking 3.0 with 5.0
Checking 3.0 with 3.0
Checking 5.0 with 3.0
Checking 5.0 with 5.0
Checking 5.0 with 3.0
Checking 3.0 with 3.0
Checking 3.0 with 5.0
Checking 3.0 with 3.0
As you can see this follows the O(n²) algorithm which is not what I'm going for. In my original code I was "skipping" elements that I already checked by using the indexes from the outer nested for loop. So my question is, if there is a way to somehow implement this in the nested <Stream>.anyMatch(...)
that I attempted. Or is there a cleaner way of doing this?
java java-8 java-stream
are you trying to replicate theduplicate
method but with streams API? with the same time complexity or better?
– Aomine
Nov 16 '18 at 23:39
@Aomineduplicate
was just a name I randomly thought of for the method, so no.
– newbie
Nov 16 '18 at 23:43
not talking about the method name, I am asking is the logic for the duplicate method what you're trying to replicate? or are you simply trying to improve upon the code snippetArrays.stream(d).anyMatch(cur -> {.........
?
– Aomine
Nov 16 '18 at 23:46
@Aomine Ah okay, sorry I misunderstood. So, yes that is correct, I am trying to replicate the logic in theduplicate
method on the last code snippet
– newbie
Nov 16 '18 at 23:47
my first question or the second.. ?
– Aomine
Nov 16 '18 at 23:47
|
show 1 more comment
I have the following 3 files,
A.java:
class A {
private float b;
public A(float b) {
this.b = b;
}
public float getB() {
return b;
}
public String toString() {
return "A(b = " + b + ")";
}
}
C.java:
import java.util.Arrays;
class C {
private A d;
private int i = 0;
public C() {
d = new A[5];
}
public void addB(A b) {
d[i++] = b;
}
public String toString() {
return "C(b = " + Arrays.toString(d) + ")";
}
public void duplicate() {
A temp = Arrays.copyOf(d, d.length);
for (int cur = 0; cur < d.length; cur++) {
if (d[cur] == null) continue;
float currB = d[cur].getB();
for (int nxt = cur + 1; nxt < d.length; nxt++) {
if(d[nxt] == null) continue;
if(currB == d[nxt].getB()) {
temp[i++] = new A(currB * 0.5f);
}
}
}
d = temp;
}
}
D.java:
class D {
public static void main(String args) {
C c = new C();
c.addB(new A(3));
c.addB(new A(5));
c.addB(new A(3));
System.out.println(c.toString()); // C(b = [A(b = 3.0), A(b = 5.0), A(b = 3.0), null, null])
c.duplicate();
System.out.println(c.toString()); // C(b = [A(b = 3.0), A(b = 5.0), A(b = 3.0), A(b = 1.5), null])
}
}
This does what I expected it to do, which is add another item to the array with half the b
if two of the elements have the same returned float from A.getB()
. However, I was trying to implement this using the fancy Java 8 stream methods and lambda functions, like so:
Arrays.stream(d).anyMatch(cur -> {
if (cur == null) return false;
Arrays.stream(d).anyMatch(nxt -> {
if (nxt == null) return false;
System.out.println("Checking " + cur.getB() + " with " + nxt.getB());
return false;
});
return false;
});
And this outputted:
Checking 3.0 with 3.0
Checking 3.0 with 5.0
Checking 3.0 with 3.0
Checking 5.0 with 3.0
Checking 5.0 with 5.0
Checking 5.0 with 3.0
Checking 3.0 with 3.0
Checking 3.0 with 5.0
Checking 3.0 with 3.0
As you can see this follows the O(n²) algorithm which is not what I'm going for. In my original code I was "skipping" elements that I already checked by using the indexes from the outer nested for loop. So my question is, if there is a way to somehow implement this in the nested <Stream>.anyMatch(...)
that I attempted. Or is there a cleaner way of doing this?
java java-8 java-stream
I have the following 3 files,
A.java:
class A {
private float b;
public A(float b) {
this.b = b;
}
public float getB() {
return b;
}
public String toString() {
return "A(b = " + b + ")";
}
}
C.java:
import java.util.Arrays;
class C {
private A d;
private int i = 0;
public C() {
d = new A[5];
}
public void addB(A b) {
d[i++] = b;
}
public String toString() {
return "C(b = " + Arrays.toString(d) + ")";
}
public void duplicate() {
A temp = Arrays.copyOf(d, d.length);
for (int cur = 0; cur < d.length; cur++) {
if (d[cur] == null) continue;
float currB = d[cur].getB();
for (int nxt = cur + 1; nxt < d.length; nxt++) {
if(d[nxt] == null) continue;
if(currB == d[nxt].getB()) {
temp[i++] = new A(currB * 0.5f);
}
}
}
d = temp;
}
}
D.java:
class D {
public static void main(String args) {
C c = new C();
c.addB(new A(3));
c.addB(new A(5));
c.addB(new A(3));
System.out.println(c.toString()); // C(b = [A(b = 3.0), A(b = 5.0), A(b = 3.0), null, null])
c.duplicate();
System.out.println(c.toString()); // C(b = [A(b = 3.0), A(b = 5.0), A(b = 3.0), A(b = 1.5), null])
}
}
This does what I expected it to do, which is add another item to the array with half the b
if two of the elements have the same returned float from A.getB()
. However, I was trying to implement this using the fancy Java 8 stream methods and lambda functions, like so:
Arrays.stream(d).anyMatch(cur -> {
if (cur == null) return false;
Arrays.stream(d).anyMatch(nxt -> {
if (nxt == null) return false;
System.out.println("Checking " + cur.getB() + " with " + nxt.getB());
return false;
});
return false;
});
And this outputted:
Checking 3.0 with 3.0
Checking 3.0 with 5.0
Checking 3.0 with 3.0
Checking 5.0 with 3.0
Checking 5.0 with 5.0
Checking 5.0 with 3.0
Checking 3.0 with 3.0
Checking 3.0 with 5.0
Checking 3.0 with 3.0
As you can see this follows the O(n²) algorithm which is not what I'm going for. In my original code I was "skipping" elements that I already checked by using the indexes from the outer nested for loop. So my question is, if there is a way to somehow implement this in the nested <Stream>.anyMatch(...)
that I attempted. Or is there a cleaner way of doing this?
java java-8 java-stream
java java-8 java-stream
edited Nov 16 '18 at 23:39
Aomine
41.8k74071
41.8k74071
asked Nov 16 '18 at 23:28
newbienewbie
672214
672214
are you trying to replicate theduplicate
method but with streams API? with the same time complexity or better?
– Aomine
Nov 16 '18 at 23:39
@Aomineduplicate
was just a name I randomly thought of for the method, so no.
– newbie
Nov 16 '18 at 23:43
not talking about the method name, I am asking is the logic for the duplicate method what you're trying to replicate? or are you simply trying to improve upon the code snippetArrays.stream(d).anyMatch(cur -> {.........
?
– Aomine
Nov 16 '18 at 23:46
@Aomine Ah okay, sorry I misunderstood. So, yes that is correct, I am trying to replicate the logic in theduplicate
method on the last code snippet
– newbie
Nov 16 '18 at 23:47
my first question or the second.. ?
– Aomine
Nov 16 '18 at 23:47
|
show 1 more comment
are you trying to replicate theduplicate
method but with streams API? with the same time complexity or better?
– Aomine
Nov 16 '18 at 23:39
@Aomineduplicate
was just a name I randomly thought of for the method, so no.
– newbie
Nov 16 '18 at 23:43
not talking about the method name, I am asking is the logic for the duplicate method what you're trying to replicate? or are you simply trying to improve upon the code snippetArrays.stream(d).anyMatch(cur -> {.........
?
– Aomine
Nov 16 '18 at 23:46
@Aomine Ah okay, sorry I misunderstood. So, yes that is correct, I am trying to replicate the logic in theduplicate
method on the last code snippet
– newbie
Nov 16 '18 at 23:47
my first question or the second.. ?
– Aomine
Nov 16 '18 at 23:47
are you trying to replicate the
duplicate
method but with streams API? with the same time complexity or better?– Aomine
Nov 16 '18 at 23:39
are you trying to replicate the
duplicate
method but with streams API? with the same time complexity or better?– Aomine
Nov 16 '18 at 23:39
@Aomine
duplicate
was just a name I randomly thought of for the method, so no.– newbie
Nov 16 '18 at 23:43
@Aomine
duplicate
was just a name I randomly thought of for the method, so no.– newbie
Nov 16 '18 at 23:43
not talking about the method name, I am asking is the logic for the duplicate method what you're trying to replicate? or are you simply trying to improve upon the code snippet
Arrays.stream(d).anyMatch(cur -> {.........
?– Aomine
Nov 16 '18 at 23:46
not talking about the method name, I am asking is the logic for the duplicate method what you're trying to replicate? or are you simply trying to improve upon the code snippet
Arrays.stream(d).anyMatch(cur -> {.........
?– Aomine
Nov 16 '18 at 23:46
@Aomine Ah okay, sorry I misunderstood. So, yes that is correct, I am trying to replicate the logic in the
duplicate
method on the last code snippet– newbie
Nov 16 '18 at 23:47
@Aomine Ah okay, sorry I misunderstood. So, yes that is correct, I am trying to replicate the logic in the
duplicate
method on the last code snippet– newbie
Nov 16 '18 at 23:47
my first question or the second.. ?
– Aomine
Nov 16 '18 at 23:47
my first question or the second.. ?
– Aomine
Nov 16 '18 at 23:47
|
show 1 more comment
1 Answer
1
active
oldest
votes
You can replicate the duplicate
method using the Stream API as follows:
Stream<A> result =
IntStream.range(0, d.length)
.filter(cur -> d[cur] != null)
.flatMap(cur -> IntStream.range(cur + 1, d.length)
.filter(nxt -> d[nxt] != null)
.filter(nxt -> d[cur].getB() == d[nxt].getB())
.map(i -> cur))
.mapToObj(cur -> new A(d[cur].getB() * 0.5f));
d = Stream.concat(Arrays.stream(d), result)
.toArray(A::new);
Thank you. Could you please briefly explain what the purpose ofIntStream.range(cur + 1, d.length)
is please?
– newbie
Nov 17 '18 at 0:04
3
it simply generates anIntStream
of numbers fromcur+1
inclusive tod.length
exclusive i.e. just like in your codefor (int nxt = cur + 1; nxt < d.length; nxt++) {...}
@newbie
– Aomine
Nov 17 '18 at 0:07
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%2f53346688%2fdeep-comparing-each-element-in-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can replicate the duplicate
method using the Stream API as follows:
Stream<A> result =
IntStream.range(0, d.length)
.filter(cur -> d[cur] != null)
.flatMap(cur -> IntStream.range(cur + 1, d.length)
.filter(nxt -> d[nxt] != null)
.filter(nxt -> d[cur].getB() == d[nxt].getB())
.map(i -> cur))
.mapToObj(cur -> new A(d[cur].getB() * 0.5f));
d = Stream.concat(Arrays.stream(d), result)
.toArray(A::new);
Thank you. Could you please briefly explain what the purpose ofIntStream.range(cur + 1, d.length)
is please?
– newbie
Nov 17 '18 at 0:04
3
it simply generates anIntStream
of numbers fromcur+1
inclusive tod.length
exclusive i.e. just like in your codefor (int nxt = cur + 1; nxt < d.length; nxt++) {...}
@newbie
– Aomine
Nov 17 '18 at 0:07
add a comment |
You can replicate the duplicate
method using the Stream API as follows:
Stream<A> result =
IntStream.range(0, d.length)
.filter(cur -> d[cur] != null)
.flatMap(cur -> IntStream.range(cur + 1, d.length)
.filter(nxt -> d[nxt] != null)
.filter(nxt -> d[cur].getB() == d[nxt].getB())
.map(i -> cur))
.mapToObj(cur -> new A(d[cur].getB() * 0.5f));
d = Stream.concat(Arrays.stream(d), result)
.toArray(A::new);
Thank you. Could you please briefly explain what the purpose ofIntStream.range(cur + 1, d.length)
is please?
– newbie
Nov 17 '18 at 0:04
3
it simply generates anIntStream
of numbers fromcur+1
inclusive tod.length
exclusive i.e. just like in your codefor (int nxt = cur + 1; nxt < d.length; nxt++) {...}
@newbie
– Aomine
Nov 17 '18 at 0:07
add a comment |
You can replicate the duplicate
method using the Stream API as follows:
Stream<A> result =
IntStream.range(0, d.length)
.filter(cur -> d[cur] != null)
.flatMap(cur -> IntStream.range(cur + 1, d.length)
.filter(nxt -> d[nxt] != null)
.filter(nxt -> d[cur].getB() == d[nxt].getB())
.map(i -> cur))
.mapToObj(cur -> new A(d[cur].getB() * 0.5f));
d = Stream.concat(Arrays.stream(d), result)
.toArray(A::new);
You can replicate the duplicate
method using the Stream API as follows:
Stream<A> result =
IntStream.range(0, d.length)
.filter(cur -> d[cur] != null)
.flatMap(cur -> IntStream.range(cur + 1, d.length)
.filter(nxt -> d[nxt] != null)
.filter(nxt -> d[cur].getB() == d[nxt].getB())
.map(i -> cur))
.mapToObj(cur -> new A(d[cur].getB() * 0.5f));
d = Stream.concat(Arrays.stream(d), result)
.toArray(A::new);
answered Nov 16 '18 at 23:53
AomineAomine
41.8k74071
41.8k74071
Thank you. Could you please briefly explain what the purpose ofIntStream.range(cur + 1, d.length)
is please?
– newbie
Nov 17 '18 at 0:04
3
it simply generates anIntStream
of numbers fromcur+1
inclusive tod.length
exclusive i.e. just like in your codefor (int nxt = cur + 1; nxt < d.length; nxt++) {...}
@newbie
– Aomine
Nov 17 '18 at 0:07
add a comment |
Thank you. Could you please briefly explain what the purpose ofIntStream.range(cur + 1, d.length)
is please?
– newbie
Nov 17 '18 at 0:04
3
it simply generates anIntStream
of numbers fromcur+1
inclusive tod.length
exclusive i.e. just like in your codefor (int nxt = cur + 1; nxt < d.length; nxt++) {...}
@newbie
– Aomine
Nov 17 '18 at 0:07
Thank you. Could you please briefly explain what the purpose of
IntStream.range(cur + 1, d.length)
is please?– newbie
Nov 17 '18 at 0:04
Thank you. Could you please briefly explain what the purpose of
IntStream.range(cur + 1, d.length)
is please?– newbie
Nov 17 '18 at 0:04
3
3
it simply generates an
IntStream
of numbers from cur+1
inclusive to d.length
exclusive i.e. just like in your code for (int nxt = cur + 1; nxt < d.length; nxt++) {...}
@newbie– Aomine
Nov 17 '18 at 0:07
it simply generates an
IntStream
of numbers from cur+1
inclusive to d.length
exclusive i.e. just like in your code for (int nxt = cur + 1; nxt < d.length; nxt++) {...}
@newbie– Aomine
Nov 17 '18 at 0:07
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%2f53346688%2fdeep-comparing-each-element-in-an-array%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
are you trying to replicate the
duplicate
method but with streams API? with the same time complexity or better?– Aomine
Nov 16 '18 at 23:39
@Aomine
duplicate
was just a name I randomly thought of for the method, so no.– newbie
Nov 16 '18 at 23:43
not talking about the method name, I am asking is the logic for the duplicate method what you're trying to replicate? or are you simply trying to improve upon the code snippet
Arrays.stream(d).anyMatch(cur -> {.........
?– Aomine
Nov 16 '18 at 23:46
@Aomine Ah okay, sorry I misunderstood. So, yes that is correct, I am trying to replicate the logic in the
duplicate
method on the last code snippet– newbie
Nov 16 '18 at 23:47
my first question or the second.. ?
– Aomine
Nov 16 '18 at 23:47