Java: What data structure should be used to simulate associative arrays of PHP?

I need a data structure that allows me to map keys to values, such as PHP associative arrays. Each key can exist only once, but the value can be matched to any number of keys. What am I looking for? Something in the Google Commons Collections?

+3
source share
4 answers

Structure Mapis what you want. A good implementation is HashMap.

This data type does not allow you to use the same value for the key, but you can have as many duplicate values ​​as possible.

Usage example:

Map<String, String> map = new HashMap<String, String>();
map.put("FirstName", "LastName");

System.out.println(map.get("FirstName")); // Prints 'LastName'
System.out.println(map.put("FirstName", "Foo")); // Prints 'LastName'
System.out.println(map.get("FirstName")); // Prints 'Foo'

, . .

+7
+4

HashMap? - , , , ,

0

HashMap - . -, PHP. , HashMap? .

0

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


All Articles