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());
Related
This question already has answers here:
Java associative-array
(15 answers)
Closed 2 years ago.
I'm beginner in Java environment, accustomed to PHP.
And in Php we can create an array like the code below :
$array['params1'] = 'the first param';
$array['params2'] = 'the second param';
And when i will output $array['params1'] it will be 'the first param'.
But i do not find any similar solutions in Java, do you know something similar ?
Thanks in advance
As #mrblewog said, you might want to readup on data structures and the syntax in Java as it is quite different than php.
To give you an Example:
// Key Value
HashMap<String, String> map = new HashMap<>();
map.insert("key1", "value1");
map.get("key1"); // returns "value1"
If you want to store other objects than Strings you will need to change the generic types (written in the <X, Y>).
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:
Does Java have support for multiline strings?
(43 answers)
Closed 7 years ago.
For JAVA development I need writing to a files with strings like "\r\t\n <>", because from Java I want writing a PHP file. If you can't understand look at this example:
BufferedWriter buffW = new BufferedWriter(fileW);
buffW.write("<?php\n\n\tclass MyClass {\n\tpublic function test()\n}\n}\n?>");
This is mess a code, I want to write a clean such as PHP can do, but can't find on Java alternative way as example:
<?php
$mystring = <<<EOT
This is some PHP text.
It is completely free
I can use "double quotes"
and 'single quotes',
plus $variables too, which will
be properly converted to their values,
you can even type EOT, as long as it
is not alone on a line, like this:
EOT;
?>
Is there a heredoc equivalent in Java?
No, there is no direct heredoc alternative in the Java programming language.
Check this similar question.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Raw Strings in Java?
In C# there is such a thing as # ("at sign") that can be put before string if forbidden symbols occurs. For example:
#"a\b\c"
In java I have to put backslashes
"a\\b\\c"
Is there any way in Java to make this easier?
Another way may be use equvivalent code for the symbols you want to escape.
Not really. I have made the transition not long ago and at first was constantly looking for "what is C#'s equivalent in Java for xyz?"
This is sometimes helpful but mostly frustrating. C# is a much more advanced language than Java and it will take a long time for Java to catch up.
You get used to it over time :-)