javaphparrayshashmap

What is Java equivalent for PHP array with non-numeric keys?


What would be Java equivalent for php array like this:

$user = array("name" => "John", "email" => "john@mail.com");

Solution

  • You can use a HashMap or a Hashtable. Not sure which one to use? Read the API or check out this question which discusses the pros and cons of each.

    HashMap<String, String> map = new HashMap<String, String>();
    map.put("name", "John");
    map.put("email", "john@mail.com");