Hot answers tagged

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 ...
user avatar
  • 12.4k
37 votes
Accepted

Filtering out empty Optionals

It's more idiomatic to use .map on the stream instead of Collectors.mapping: ...
user avatar
  • 994
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 ...
user avatar
  • 96.7k
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 ...
user avatar
  • 96.7k
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: ...
user avatar
  • 29.7k
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 ...
user avatar
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))
user avatar
  • 401
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 ...
user avatar
  • 96.7k
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. ...
user avatar
  • 96.7k
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 <...
user avatar
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 ...
user avatar
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 ...
user avatar
  • 156
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 ...
user avatar
  • 10.6k
11 votes
Accepted

Object-to-XML string implementation

StringWriter and XmlWriter are both IDisposable hence their usage should be wrapped in a <...
user avatar
  • 20k
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 ...
user avatar
  • 5,463
11 votes
Accepted

Determining winner and winning hand in poker (holdem)

Card The names SUIT and RANK should be Suit and Rank. I'...
user avatar
  • 13.4k
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 ...
user avatar
  • 10.3k
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,...
user avatar
  • 10.6k
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 ...
user avatar
  • 107k
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 ...
user avatar
  • 10.6k
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 ...
user avatar
  • 64.1k
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 ...
user avatar
  • 9,195
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 ...
user avatar
  • 4,682
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 ...
user avatar
  • 96.7k
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...
user avatar
  • 19.8k
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. ...
user avatar
  • 107k
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 <...
user avatar
  • 49.9k
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 ...
user avatar
  • 8,498
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. ...
user avatar
  • 91.5k
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 ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible