71
votes
Accepted
Is it OK to use while ((line = r.readLine()) != null) construct?
Assignment inside a condition is ok in this case, as the assignment is surrounded by an extra pair of parentheses – the comparison is obviously != null, there is no ...
37
votes
Accepted
Filtering out empty Optionals
It's more idiomatic to use .map on the stream instead of Collectors.mapping:
...
29
votes
Accepted
StreamIterable - create an iterable from a Java 8 Stream
Two things:
I think you have missed the native implementation. Are you intentionally re-inventing the wheel? Streams have an iterator() method.
Note that streams cannot be reversed, so, while you can ...
22
votes
Accepted
Spliterator implementation
I see a few, actually, a number of problems in this code. I am afraid this will be something of a scathing review, in part because you mention this is intended to be for a tutorial....
I see a number ...
21
votes
Is it OK to use while ((line = r.readLine()) != null) construct?
You could increase the abstraction level of the code a little bit with an iterator-like pattern and the same time you could reuse an existing library (with the experience of the authors) for that: ...
20
votes
StreamIterable - create an iterable from a Java 8 Stream
You should not need a special class for this. Method references can be cast to functional interfaces. In this particular case, however, remember that the stream returned by ...
16
votes
Finding the longest string and its length using Java streams
You don't need the Pair. You can get the String with the maximum length by doing
names.stream().max(Comparator.comparingInt(String::length))
15
votes
Accepted
Effective use of multiple streams
Your code is odd in the sense that it is going to a lot of effort to calculate that 103 is 1000.
I understand why you are doing it, but I took the liberty of changing the ...
14
votes
Accepted
Using Java 8 parallel streams
Your code is not thread-safe. Each of the threads will, in parallel, be accessing both the best, and the bestQuality variables.
...
13
votes
Is it OK to use while ((line = r.readLine()) != null) construct?
Instead of wrapping the reader in an iterator, you could also wrap it in an Iterable that then returns the iterator.
It would allow you to write the following
<...
12
votes
Streaming int support
After reading the problem description and before reading your code, I implemented something that was very similar to what you already have, which means that your approach to the problem is perfectly ...
12
votes
Accepted
Mandelbrot Streams
Say what you mean and mean what you say
You named your class "Window" when it should really be something more like "Viewport". There are no "Windows" (in the GUI sense) in your code. If you decide to ...
11
votes
Effective use of multiple streams
I do not have much to add to @rofl's answer, but I do have another way to show to create Cartesian Products while using streams, which I think might be very interesting aswell.
Important note
I have ...
11
votes
Accepted
Object-to-XML string implementation
StringWriter and XmlWriter are both IDisposable hence their usage should be wrapped in a <...
11
votes
Accepted
Automatic flowchart generator
You're a Python native, aren't you? I can tell from the """ multiline strings and the four-space indents and really unidiomatic whitespacing and ...
11
votes
Accepted
Determining winner and winning hand in poker (holdem)
Card
The names SUIT and RANK should be Suit and Rank. I'...
11
votes
Accepted
Counting relevant entries in a large bioinformatics file
In order to speed this up, you'll need to avoid as many string creation operations as possible, because they are expensive. Especially the split operation is expensive. Not only does this create many ...
10
votes
Compare values of elements in Stream
Your code and Java 8 usage looks fine in general to me.
I do see an issue with the Person class, it looks like you are intending it to be an immutable class, if so,...
10
votes
Accepted
Compare values of elements in Stream
There cannot be a simpler solution:
To know the duplicated ids, you must iterate over the entire collection.
To print all the persons with duplicated ids, you must keep their full list.
As such, you ...
10
votes
Accepted
Pi by Monte-Carlo
The code and documentation look fine in general, so this review will focus on minor optimizations on a per-method basis.
Use ThreadLocal.withInitial
You can set ...
10
votes
Accepted
C++ console program
I see a number of things that could help you improve your code.
Decompose your program into functions
All of the logic here is in main in one chunk of code. It ...
10
votes
Printing alternating characters of a string using Java 8 streams
The main issue with your code is that it operates with side-effects by leveraging the forEach operation. If you were to run your code in parallel, it would be ...
10
votes
Counting relevant entries in a large bioinformatics file
If you are in for raw performance, try to avoid repeating potentially cost-intensive operations.
In this case, you split the lines twice with the same parameter, which repeatedly applies a regular ...
9
votes
Compare values of elements in Stream
This problem space is not a 'textbook' match to the streams concept. There are going to be some rough edges. The most significant thing is that System.out.println ...
8
votes
Accepted
Deviating from the norm
As usual, a few points to complete what has already been said:
An std::ifstream is implicitly constructed with std::ios_base::in...
8
votes
Accepted
The answer to life, the universe, and Project Euler
Processing chunk-by-chunk vs line-by-line
Obviously, if the input file was of 1TB size,
containing a single line full of words,
your computer probably wouldn't have enough memory to run this program.
...
8
votes
Accepted
Packing and unpacking bits
Bug
The swap<T>() method isn't doing what you think it does, because the passed in T obj1, T obj2 aren't passed with the <...
8
votes
Accepted
Replace EOL in stream
Performance
The non-Async methods are much faster in case of ReadLine/Write. If you need to run the code in background, use ...
8
votes
Accepted
Memory streambuf and stream
Assert
I personally dislike using asserts. The problem for me is that they do different things in production and debug code. I want the same action in both.
...
8
votes
Accepted
Overloading istream>> to read comma-separated input
95 percent of programming is looking for redundancies and eliminating them.
For example, why do you bother with reading strings into accusations[] first, and then ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
stream × 307java × 117
c++ × 86
c# × 54
io × 20
performance × 18
file × 18
strings × 15
c++11 × 14
lambda × 13
beginner × 12
functional-programming × 11
algorithm × 10
c × 10
c++14 × 10
socket × 10
parsing × 9
node.js × 9
formatting × 9
serialization × 9
javascript × 8
tcp × 7
python × 6
.net × 6
console × 6