Deep comparing each element in an array












3















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?










share|improve this question

























  • 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
















3















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?










share|improve this question

























  • 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














3












3








3


1






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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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

















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












1 Answer
1






active

oldest

votes


















3














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





share|improve this answer
























  • 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





    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













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









3














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





share|improve this answer
























  • 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





    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


















3














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





share|improve this answer
























  • 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





    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
















3












3








3







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 23:53









AomineAomine

41.8k74071




41.8k74071













  • 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





    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





















  • 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





    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



















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




















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%2f53346688%2fdeep-comparing-each-element-in-an-array%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

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)