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>).
Related
This question already has answers here:
Simplest way to add an item to beginning of an array in Java
(9 answers)
Closed 9 months ago.
I am new to Java and I have an array
int [] A = {140,150,160,170,180,190}
I need to add infinity at the beginning of the array
like so:
int [] A = {inf,140,150,160,170,180,190}
I know from Python to do it like so:
A=[140,150,160,170,180,190]
A=[float('inf')]+A
I need a way to do that in Java as simple as Python without loops
It's important to say I need the array / list(A=[140,150,160,170,180,190]) to be given with the code.
You can use Java ArrayList that implements List interface and has a method add(int,E)
So, you can use something like list.add(0, myObj);.
This question already has answers here:
A quick and easy way to join array elements with a separator (the opposite of split) in Java [duplicate]
(15 answers)
Best way to convert an ArrayList to a string
(27 answers)
What's the simplest way to print a Java array?
(37 answers)
Java function for arrays like PHP's join()?
(24 answers)
Closed 3 years ago.
I have an ArrayList<String> that is added to periodically. What I want to do is to cast the entire ArrayList to a String without doing a loop. Can anybody tell me is it possible without using loop?
Edited:
So we found some solutions, Like
list.stream().collect(Collectors.joining());
Or
String result = String.join(",", list);
or some others as well. Now Just for getting knowledge I put a question which one is the most optimal way for compiler?
You could make a stream out of your list and collect it using joining collector :
list.stream().collect(Collectors.joining());
You can pass the separator to Collectors::joining method for example :
list.stream().collect(Collectors.joining("-"));
Or you can use String::join method :
String result = String.join(",", list);
where , is the separator.
I Believe you can do
String.join(", ", list);
Hope this helps :)
Given some ArrayList<String> s, all you need to use to get a String representation is s.toString().
The format is the following
[element1, element2, element3]
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:
Fuzzy string search library in Java [closed]
(8 answers)
Closed 6 years ago.
I am working on a game where the user has to fill in a name of a celebrity. If the name is not 100% correct but nearly correct, the compare should succeed. Is there a ready to use function in java or something someone ever has written so I can use it ?
What you are looking for is the Levenshtein algortithm
You'll find here some Java implementations : https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Java
Or, if you don't want/need to understand how it works, you can get the score directly from Apache StringUtils : getLevenshteinDistance
And if you want to get the percentage of similarities, you can do :
int lev = StringUtils.getLevenshteinDistance(s1, s2);
double ratio = ((double) lev) / (Math.max(s1.length, s2.length));
This question already has answers here:
Sorting HashMap by values [duplicate]
(12 answers)
Closed 9 years ago.
I have a java Map
Map<String , String>
The map is like following
olah : 3
vola : 2
sola : 5
jingle : 9
i want to sort the map on the value string like sort on 3,2,5,9 for example...is there any efficient way possible.
I also want to know what difference it will make if i put a map with same values but
like
Map<String , long>
Does it improve any performance...?
See: Sort a Map<Key, Value> by values (Java)
Something like
Map<String , long>
is not possible because Java Collections can not store primitive types. If you want to do that, you can use fastutil http://fastutil.di.unimi.it/ or trove http://trove.starlight-systems.com/. Memory consumption will be better, since no Long objects are created.