Anil Philip wrote:I don't know why the compiler complains about the ternary operator: "not a statement".
See Campbell's reply below for some clarifications.
Anil Philip wrote:How would I do it as part of the lambda in the stream?
Well, fields are allowed to change, but not parameters and local variables. You probably wouldn't want those three variables to be fields. You could create a local class to do the counting, and that will compile. But if a local class isn't overkill, I don't know what is!Ron McLeod wrote:. . . variables in a lambda expression need to be effectively final . . .
You use that to get a Collector which you pass to the Stream#collect() method. I lost that part when editing my post.A few minutes ago, I wrote:. . . You can use groupingBy(). . . .
Ron McLeod wrote:Your example will not compile because forEach expects to operate on a Consumer, and a Consumer cannot return a value.
If you wrapped it with something like System.out.println like Liutauras showed before it will also fail to compile because variables in a lambda expression need to be effectively final, and positives, zeroes, and negatives are not.
Fields can be either static type or instance type (sometimes called non‑static), but not both. Those are static fields. Didn't HackerRank tell you that static variables are usually a bad idea?Anil Philip wrote:. . . static instance variables. . . .
Campbell Ritchie wrote:
Fields can be either static type or instance type (sometimes called non‑static), but not both. Those are static fields. Didn't HackerRank tell you that static variables are usually a bad idea?Anil Philip wrote:. . . static instance variables. . . .
Anil Philip wrote:In the code sample, they are static instance variables.
Liutauras Vilda wrote:
Anil Philip wrote:In the code sample, they are static instance variables.
I registered to that platform and I couldn't find them. You are asked to implement plusMinus() function that returns nothing, it should just print result as requested. I've passed the tests with my almost original code. I didn't know about ratios.
Campbell Ritchie wrote:The HackerRank link says to calculate the proportions, which you can do with something like 1.0 * positives / total. The 1.0 * part converts the numbers to doubles.
Carey Brown wrote:@Anil,
You made "positives" (etc) float in order to have a floating point result from the divide. (Almost) Never use "float", use "double" instead it is the internal floating point default type and is more efficient. "float" should only be used when interfacing with legacy code.
Additionally, making those three variables float incurs some overhead for every increment (++) operation, change them to long. Then change "n" to "double". Now the results of the divisions will be floating point without the increment penalty.
Paul Clapham wrote:I decided to see if you could use a method reference to rehabilitate your code. And yes, you can. Here's my revised version:Note the unnecessary "whatever" variable there, it is actually necessary to allow the code to compile, for the reasons already discussed. You'll get a warning from the compiler, though. But at least it works in this context and it's a minimal change from your original try. And besides, method references are pure functional programming.
As you see, even static methods can be used as the target of a method reference. (I assumed that your class was named "Test".) That's a bit in bad taste, though, it's generally recommended to eschew static methods as much as possible.
Carey Brown wrote:There's nothing in Hackerrank that requires that streams or lambdas be used.
Carey Brown wrote:There's nothing in Hackerrank that requires that streams or lambdas be used.
Yes, I didn't put in @SuppressWarnings to take care of that. But the maintainer who removed the declaration part would then be facing the compiler error which was the topic of this thread. They could ask about it here, we're always ready to help.Carey Brown wrote:
As I said, do your counting with integer types. I don't like to see floats anywhere because of their poor precision.Anil Philip wrote:. . . if you start with a float, . . .
Nor @SuppressWarnings. That template looks like pre‑Java5 codeCarey Brown wrote:There's nothing in Hackerrank that requires that streams or lambdas be used.
Not without logging on to that site, I am afraid. Just as well you supplied a copy for us.Anil Philip wrote:. . . Are you able to see Plus Minus . . . ? . . .
Ron McLeod wrote:Your example will not compile because forEach expects to operate on a Consumer, and a Consumer cannot return a value.
If you wrapped it with something like System.out.println like Liutauras showed before it will also fail to compile because variables in a lambda expression need to be effectively final, and positives, zeroes, and negatives are not.
Did anybody notice the logic error in what I posted?Last night, I wrote:. . . Reading from the keyboard would be much easier with a Scanner object. . . .
Stephan van Hulst wrote:Great, you made it work. Now, never write code like that ever again.
Either use a simple for-loop, or use a collector.
For that sort of thing, always look in the documentation. Never trry to learn what the documentation says; you won't remember anything used less frequently than Math.max() or System.out.printf(). If you look at the documentation for Consumer, you will find it has one abstract method, and that link will show you it has void instead of a return type. The fact that it says void means it can't return anything.Anil Philip wrote:. . . I did not know that you must not use an expression that returns a value in a Consumer lambda.
That's a pleasureThanks. . . .
Except that, if you sit a certification exam, the exam syllabus will expect you to learn what the documentation says.A minute ago, I wrote:. . . Never try to learn what the documentation says . . .
Carey Brown wrote:I was thinking of something more preemptive.
Anil Philip wrote:
Stephan van Hulst wrote:Great, you made it work. Now, never write code like that ever again.
Either use a simple for-loop, or use a collector.
Huh? I am quite proud of what I wrote.![]()
Liutauras Vilda wrote:
What I mentioned earlier to Anil, I registered yesterday to hackerank to check the problem at hand and I couldn't see in the provided template any static variables. So I'm not sure where they came from. I solved that problem by using collector to collect results and then did printing them out (didn't use any static variables), and I'd suggest Anil to refactor his code too.
.
Not true. A simple loop, for example, would not have needed static member variables. It could use local variables.Anil Philip wrote:The method provided by HackerRank was static and so I had to use static member variables.
I already have.Anil Philip wrote:. . . Can you please share your code using collectors? . . .
You could have used local variables as long as you didn't use them in a λ or anonymous class. I think the compiler error message about “non‑static” is less than crystal‑clear, and confuses people into making things static that oughtn't to be static.The method provided by HackerRank was static and so I had to use static member variables.
Carey Brown wrote:
Not true. A simple loop, for example, would not have needed static member variables. It could use local variables.Anil Philip wrote:The method provided by HackerRank was static and so I had to use static member variables.
Everyone is a villain in someone else's story. Especially this devious tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|