Tag Archives: list

PHP Arrays in JAVA

Going from PHP to Java is a step backwards when dealing with arrays (imo).  One of the more beneficial things that you can do with a PHP array is define keys.  For example I can do something like:

$myList = array("oranges"=>15, "apples"=>25);

Then I can access how many oranges I have by going echo $myList["oranges"];

To do something like this in JAVA is a bit different than just defining a normal array. In JAVA you need to use a HashMap:

HashMap<String,Integer> myList = new HashMap<String,Integer>();
myList.put("oranges", 15);
myList.put("apples", 25);
If you need to store two strings, then change the object type (I.e. HashMap<String,String>).
To get the value, simply use the get method.