Pros and Cons of usage forEach and Stream [duplicate] - java

This question already has answers here:
What is difference between Collection.stream().forEach() and Collection.forEach()?
(5 answers)
Closed 5 years ago.
Starts from java 8 to iterate throug list I can use both:
List list = new ArrayList();
1. list.forEach(...)
2. list.stream().forEach(...)
Is it any advantages of using second case? To convert list to stream?

There are no advantages of using the second case, unless you have a parallel stream. There is a disadvantage, namely that Stream.forEach() doesn't guarantee to respect encounter order. A more accurate (but still unnecessary) equivalent would be Stream.forEachOrdered().

No, in theory, the second option is worse than the first one - you pay the cost of instantiation/garbage-collection and calling a Stream instance and don't really get any benefit in return.
Additionally, in theory the iteration order of Stream.forEach() isn't deterministic.

Related

Is it possible to create and infinite stream without limiting the size? [duplicate]

This question already has answers here:
How to create an infinite stream with Java 8
(4 answers)
How to create an infinite Stream<E> out of an Iterator<E>?
(4 answers)
Closed 1 year ago.
Can we create a Java stream with an infinite source of data (e.g. Health signals)?
Yes.
Of course, limiting the size would cause the stream to be finite.
With, for example, Stream.generate(() -> "tick") you have an infinite stream.
However, the actual implementation heavily depends on the shape of the source. If, for instance, for heart beats, I expect the stream coming from some external device, so then you'll need to setup the transmission as well.

Java equivilant to pythons / haskells map() function with multiprocessing/multithreading? [duplicate]

This question already has an answer here:
Java equivalent for Python pool.map/ Multiprocessing
(1 answer)
Closed 4 years ago.
I know there have been questions which are similar to mine. However, they seem very outdated (assuming JDK 7, etc.)
So, I've been programming python for a while and had to learn Java for university.
I know that there is a feature in Python, where you can use a pool of Threads/Processes for mapping a list of values to a function.
from multiprocessing.dummy import Pool as ThreadPool
pool = ThreadPool(4)
results = pool.map(my_function, my_array)
I have to use the function on a large set of files and I have to use Java (10) and I want to use multiprocessing.
My question is: Does Java have such a feature? If so, whats the best practice to use it properly?
Yes, you can use parallelStream, for example, convert integer list to string list:
List<Integer> list = List.of(1, 2);
List<String> strings =
list.parallelStream()
.map(integer -> String.valueOf(integer)).collect(Collectors.toList());

how to split a list into a given number of sub-lists? [duplicate]

This question already has answers here:
Is there a common Java utility to break a list into batches?
(21 answers)
Closed 6 years ago.
I have a list that may be over 1000 strings, however I do not know how many exactly.
What is the best way to split this list into smaller lists without loosing any members of the list?
For example If I have a list of 1323 members, how can I best split it into 3 almost evenly sized lists?
I have seen the Guava and Commons way of splitting lists by the partition function, but that function will split the list into given size of chunks and not given number of groups (sub-lists).
Guava has a function Lists.partition which will do this for you
Usage:
Lists.partition(mylist, mylist.size()/3);

What is very faster, normal for loop or foreach loop in java? [duplicate]

This question already has answers here:
Java loop efficiency ("for" vs. "foreach") [duplicate]
(5 answers)
Closed 8 years ago.
What is very faster, normal {for loop} or {foreach loop} in java ?
They are different first of all. for-each uses Iterator under the hood whereas for is useful for operations on arrays or where you can do something meaningful with indexes.

Generate all permutations in Java [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Generating all permutations of a given string
I have an array of arbitrary length in Java, and I would like to generate all possible permutations of them. The easy way to do this for a fixed length would be a series of nested for loops, but because the array is of unknown length, that is not an option here. Is there a straightforward way to accomplish this in Java?
Use a recursive function, instead of loops. Each time you call the method should be on a smaller portion of the array and stop when length = 0. This link should help you design your function.
It may or may not be optimal as far as performance goes, but if you're looking for a way to do it with writing relatively little code and having it be clear and maintainable, you want a recursive method rather than nested loops.

Categories

Resources