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]);
Related
This question already has answers here:
Large Numbers Requiring More than 64 bit Representation
(5 answers)
Closed 2 years ago.
In a project that is related to security,
it is required to deal with very huge prime numbers such as:
3136666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666313
I am using java, and as it is know that maximum number that could be declared is 64-bits (type long)
So is there any approach to deal with these numbers?
You can use BigDecimal from java.math package:
BigDecimal longerThanLong = new BigDecimal("9223372036854775808");
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:
Java 8 Stream IllegalStateException: Stream has already been operated on or closed
(6 answers)
Closed 6 years ago.
I need to calculate the ratio of two parts of the big List, wherein the first part contains the second:
Stream<Element> part1 = list.stream().filter(x -> x.getN1() < x.getN2);
int result = part1.filter(y -> y.isRight()).count() / part1.count();
But this code throws the Exception: java.lang.IllegalStateException: stream has already been operated upon or closed
Can I write a code without creating the same part1 stream in result?
You can only reuse a collection as it has memoriation of results.
List<Element> part1 = list.stream().filter(x -> x.getN1() < x.getN2).collect(toList());
double result = (double) part1.stream().filter(y -> y.isRight()).count() / part1.size();
A Stream is a builder for some code which is optimised at run time. It's execution isn't as dynamic as it appears.
Streams are not supposed to be reused, or if you want something seemed to it, you can use suppliers as mentioned here : Copy a stream to avoid "stream has already been operated upon or closed" (java 8)
This question already has answers here:
The most efficient way to implement an integer based power function pow(int, int)
(21 answers)
Closed 8 years ago.
I had this question sent to me by a potential employer, and my answer apparently wasn't up to snuff. Can anyone help?
Implement a function that calculates power(A,B), where A and B are
positive integers, assuming there is no power function in your
programming language. Also, assume A and B are of Big Integer type so
that there is no arithmetic overflow. What is the computational
complexity of your function? Can you come up with a solution that runs
with log(B) time?
I'll just give you a hint. Suppose B is 45. Then A45 = A32 * A8 * A4 * A1. You can compute A1, A2, A4, A8, etc. by starting with A and squaring it in every iteration.
This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
I am trying to calculate the power as below but it is giving me 'bad operands type for binary operator '^'. I am guessing that it is a precedence issue but it still doesn't fix with inserting additional brackets
double pw = ((N - (df + 1))^2);
You should use java.lang.Math.pow(x,y)
Example: java.lang.Math.pow(2,3) returns 8
See this
http://www.tutorialspoint.com/java/lang/math_pow.htm
java.lang.Math.pow(double a, double b)
You can use static import for this.