rawbear1826's blog

By rawbear1826, history, 7 years ago, In English

java stream API is really cool addition to the java language (if you are a c++ hardcore fan please pass on this post :) ) i have tried to solve this problem and i tried multiple submission just to try out java stream API ... that submission was successful but this one wasn't and the only change is return a.parallelStream().allMatch(e -> e == a.get(0)); which was resulted in failure ... can anyone explain why e == a.get(0) didn't work but int c = a.get(0) and e == c did work ?

  • Vote: I like it
  • +2
  • Vote: I do not like it

»
7 years ago, # |
  Vote: I like it +6 Vote: I do not like it

It's because in java Integer is a reference data type and comparing reference datatypes using == operator does not give the same result every time.

Your code with a small change.

»
7 years ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

One more thing: parallelStream() is pretty useless on Codeforces and other online judges (and it should be less efficient than the regular stream() if you are not able to run things in parallel).

»
7 years ago, # |
  Vote: I like it -13 Vote: I do not like it

Same result in c++:

return set<int>(all(a)).size()