"John",...">

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

What is the Java equivalent for php array:

$user = array("name" => "John", "email" => "john@mail.com");
+3
source share
7 answers

You can use HashMap or Hashtable . Not sure which one to use? Read the API or see 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");
+7
source

The implementation of the Map interface is the equivalent of a Java associative array, but it looks like what you really want is a user class with fields for name and email.

+6
source

, PHP , . , foreach , .

Java, , LinkedHashMap.

+5

LinkedHashMap. HashMap , . HashMap , .

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

java.util.HashMap

+2
Map user = new HashMap();
user.put("name", "John");
user.put("email", "john@mail.com");
+1

Source: https://habr.com/ru/post/1780771/


All Articles