Java Lambdas and Streams: pass streams around [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have a fairly dumb question. We all know that stream can have a number of intermediate operations but the real computation is done only when we call some terminal operation. Is it common to pass streams around without calling terminal operation for a long time?
Let me explain what I mean. Consider the following example with Iterator/Iterable
Read file with buffered reader in lines, return Iterator with
next() overrided to call reader.readLine()
In the upper class use guava's Iterators.transform to say lowercase
everything.
In the upper class wrap with another Iterator which may be split's
the line from upstream iterator by coma and returning tuples of
words in the line
In the final class consume the iterator by iterating over it and
writing to some OutputStream.
With all that I have completely lazy computation done from the start till the end. No intermediate collections are used, etc.
If I wanted to do the same with streams I guess I should pass around the Stream object itself. Is it very common to do so? Can you share some links with me?

Related

Which is faster in java charAt() or startsWith()? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
My question is, if I want to check a character (only one character to check) of a string in a particular index which method is very efficient charAt() or startsWith(). I mean in comparison of time complexity as far I can guess startsWith() gets more time than charAt(). Because startsWith() needs to check a set of characters, but charAt() just need to check only one character.
Now tell me your opinion… what you think about which is efficient to use to check only one character?
Both methods can be used to check a specific character for its value.
charAt() directly returns the char at the requested index, startsWith(prefix, index) will return true if you provide corresponding arguments.
The major difference is that the second approach has a bit more of overhead.
So, theoretically option 1 has slightly better performance. But beyond that, you rather pick option 1 because that just does what you want in the most clear way.
The really important difference is not about performance, but about your code communicating your intent. So, albeit is possible to use startsWith() to do what you want, it is simply counterintuitive to use it that way.
if I want to check a character(Only one character to check) of a string in a particular index
You answered by yourself. If you need to check that in a particular index, you can't use startsWith() , because you can't choose the index.
They do different jobs, so , based on your question, always use charAt()

Regex best practice: multiple patterns or single one with combined expression? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Think of the following scenario:
Application receives a list of regex from server (HTTP GET returns a List with each item indicating a regular expression.);
User input text needs to be validate against these expressions;
Application runs on Android, so memory is an issue;
List of expressions is not frequently changed.
What would be better:
Cache several Pattern objects, each one containing a single regex returned from server;
Concatenate the regex - (REGEX1)|(REGEX2)|(REGEX3)...|(REGEXN) - and maintain a single object on memory? - refreshing it whenever a single regex is added or removed, which doesn't happens very often.
I don't imagine there is a way to answer this question without having a specific list of regex's and the list of input. Because, each regex/input combination is going to result in a different amount of memory used. Here is what my instincts tell me:
Evaluate the Regex's one at a time. In the "OR" scenario, the regex must simultaneously evaluate all of the OR'd expressions, so that would take more RAM, I believe.
Order the Regex's in order of either: (a) Likelihood to match, so that you can abandon evaluating the rest of the regex's or (b) Early non-matching, so that the regex can be quickly discarded as never going to match (for example "^a" only requires evaluating the first character of the string where as "a" requires searching the whole string for an "a".)
Ultimately, only testing can really tell you what takes more time/memory, I'm afraid.

Can Java ever be based on UTF-8, like 'Go' is? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Java is currently UTF-16 natively. I know there are ways to convert to UTF-8.
With Unix already being UTF-8 based (only reference it as Java is mostly run on 'nix), how difficult would it be for Java to get into the UTF-8 arena natively, like the rest of the world is leaning towards, for more efficiency?
Will it involve a total rewrite of the language ?
The problem with UTF-8 is that you cannot implement charAt method with O(1) performance. There are many code in the world which rely on this. Something like:
for(int i=0; i<string.length(); i++) {
char c = string.charAt(i);
...
}
If you switch to UTF-8, looking for i-th character will be O(n), thus such code will become O(n^2) which can become performance disaster.
As for efficiency there's a proposal to revive compressed strings in Java: strings consisting solely of ASCII-7 characters can be stored in byte[] array. As far as I know, this feature is being actively developed and the are chances that it will be included in JDK9.

Why doesn't java.lang.Short have a reverse() method similar to java.lang.Integer [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Why doesn't java.lang.Short (or Float or Double) class have a reverse method similar to java.lang.Integer ?
They both do have reverseBytes method though.
Why isn't the API list consistent ?
short can hold 2 bytes and it would have made sense to have a reverse method as well.
Wouldn't it ?
Thanks
While I agree on the API criticism, it's pretty simple to emulate:
short input = ...;
short reversed = (short)(Integer.reverse(input) >> 16);
So maybe the answer is:
Not enough people felt it was necessary
It's easy enough to simulate
Someone wanted to show off with the implementation of Integer.reverse()
Every line of code needs to maintained. Less code == less bugs, lower cost, easier maintenance.

How to reverse the SingleLinkedList in the Java? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Hi I want to print the SingleLinkedList in reverse.
static void Collections.reverse(List list):
http://download.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html
See Collections.reverse (JavaDoc)
I don't know for "The SingleLinkedList" class (is it even a List implementation ?) but if it works as its name states, the best way (more effective one) would be to go through the data structure and put every element in a stack. Then simply read the stack.
public print(Element el) {
if (el.next!=null)
print(el.next);
System.out.print(el.value+" ");
}
Basicaly its puting it in (call) stack an reading it (as someone else sugested). Nonrecursive version using explicit stack is usualy faster, does not risk StackOverflowException and takes longer to write.

Categories

Resources