This question already has answers here:
Does Java 8 provide a good way to repeat a value or function?
(6 answers)
Closed 7 years ago.
Maybe a normal for loop is still the right way but I wanted to see if there is a more succinct way to do it in java 8.
for (int i = 0; i < LIMIT; i++) {
// Code
}
Is there a more java 8 way to do this. I don't actually need i just need to repeat something x number of times.
Thanks,
Nathan
The best way I can see on how to do this would be something like IntStream.range(0, LIMIT).forEach($ -> code).
One of the reasons to use IntStream is to add parallel-ism, assuming you understand the impact of this.
IntStream.range(0, LIMIT).parallel().forEach($ -> {
// some thing thread safe.
});
Related
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());
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.
This question already has answers here:
How to add two arrays in Java in parallel manner?
(5 answers)
Closed 6 years ago.
I already found this question here, but it be great to see more options.
How to add two arrays in Java in parallel manner?
I have 2 float/ double arrays (around 10.000 to 100.000 entries) where I need to perform component wise operations on (e.g. division, multiplication, addition).
I'm working on a PC with 4 to 32 CPUs, thus I'd love to use this power and execute these computations in parallel in a Java environment.
What are good ways to do that in Java?
Thank you for your answers in advance!
Something like this?
double [] t0 = {....};
double [] t1 = {....};
double [] result =new double[t0.length];
IntStream.range(0, t0.length).parallel().forEach(i -> result[i] = t0[i] + t1[i]);
This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 8 years ago.
I came across a code snippet online that used a notation that from what I gather seems to do a comparison and then returns back possible multiple outputs. I am still confused about it, even after research. Can someone re-write the code snippet to an equivalent, more basic version so that I can make sure I am understanding what I am seeing?
int mPart = i < mParts.length ? Integer.parseInt(mParts[i]) : 0;
Thanks in advance!
This is ternary IF operator. This line is equal to
int mPart;
if(i < mParts.length) {
mPart = Integer.parseInt(mParts[i]);
} else {
mPart = 0;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicates:
Loop counter in Java API
Which of these pieces of code is faster in Java?
for(int i = 100000; i > 0; i--) {}
for(int i = 1; i < 100001; i++) {}
Which one is faster?I read that first for loop is faster.is it true?Then how it become faster than other?please help.
There is no way to tell which of the two is faster.
If all you provide is a snippet of Java code, all we have to go on is the Java Language Specification. Since the Java Language Specification never mentions any timing aspects there's no way to answer the question.
It is similar to asking your math teacher, "Which is faster to compute, 17+17 or 17*17?" Your math teacher will just stare at you and at best respond with something like, "are you using pen and paper or a pocket calculator?"