Home » Php » $key php function equivalent in java?

$key php function equivalent in java?

Posted by: admin February 25, 2020 Leave a comment


does anyone know what the equivalent of this php code would be in java?

foreach($data['elements'] as $key=>$item)

Any help welcome. Thanks

How to&Answers:

You have to understand that PHP arrays work like a hash table.
A hash table is not a Java thing but a fundamental informatics algorithm.

This is the equivalent in Java:

HashMap<String, String> m = new HashMap<String,String>();

m.put("hey", "hi");
m.put("blah", "ho");

for (Map.Entry<String, String> entry : m.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();