Java – Foreach Key Value Pair Issue


I'm trying to convert a PHP script into a Java one but coming across a few issues on a foreach loop. In the PHP script I have a foreach that takes the key:value pair and based off this does a str_replace.

  foreach ($pValues AS $vKey => $vValue)
        $vString = str_replace("{".$vKey."}", "'".$vValue."'", $vString);

I've tried replicating this ins Java without success, I need to get the key from the array to use in the string replace function but can't for the life of me find out where or if its possible to get the key name from the array passed in.

Is this the right way or am I completely off!? Should I be using the ImmutablePair method?

  for (String vKey : pValues)
        // String replace

Here's hoping there is an easy way to get the key:value pair in Java, thanks in advance.

Best Solution

This can be acheived by using Map as data structure and then using entryset for iterating over it.

 Map<K,V> entries= new HashMap<>();
    for(Entry<K,V> entry : entries.entrySet()){
        // you can get key by entry.getKey() and value by entry.getValue()
        // or set new value by entry.setValue(V value)