Reputation: 12383
I am trying to port this application from PHP to Java. PHP makes things easy with their associate name arrays and the extract() function. For Java I am thinking about using a HashMap to simulate the Arrays in PHP. Is there a better data structure to use than this? And is there a function similar to extract in Java?
Upvotes: 1
Views: 164
Reputation: 3703
HashMap will do, but there is no Java equivalent to extract(), because of the fundamentally different background of the language.
Upvotes: 1
Reputation: 82559
Yes HashMap (or TreeMap, depending on your memory needs) will work perfectly for you.
There is nothing like extract in Java.
Consider the java.util.Map
interface: It requires a Key object and a Value object.
In PHP you can take the value of a Key
and turn it into a variable name.
In Java, you are required to explicitly define your variables - you cannot define new variables with names based on the runtime values of other variables.
Upvotes: 2
Reputation: 5848
If you want to preserve the order you put the values into the HashMap, then look into using LinkedHashMap. But HashMap is a decent choice if you don't have that requirement.
http://download.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html
Upvotes: 2