How to get next key/value pair of linkedhashmap using enhanced for loop in Java

How do we get next key/value pair of a linkedhashmap using an enhanced for loop in Java?

What we want to achieve is to get a value from a linkedhashmap and pair it with the next value in the linkedhashmap. To achieve this, first of all, we have a list looking like the following:

LinkedHashMap<String, String> dates = new LinkedHashMap<String, String>();

// first quarter
dates.put("January", "2021-01-01");
dates.put("March", "2021-03-31");

// second quarter
dates.put("April", "2021-04-01");
dates.put("June", "2021-06-30");

// third quarter
dates.put("July", "2021-07-01");
dates.put("September", "2021-09-30");

// fourth quarter
dates.put("Oktober", "2021-10-01");
dates.put("December", "2021-12-31");

Above outputs:

{January=2021-01-01, March=2021-03-31, April=2021-04-01, June=2021-06-30, July=2021-07-01, September=2021-09-30, Oktober=2021-10-01, December=2021-12-31}

Then we want to select 2021-01-01 and then pair it with 2021-03-31. We then want 2021-04-01 and pair it with its next value, which is 2021-06-30 and so on… To achieve this, we have a function, where we initialize a enhanced for loop to get the values of the linkedhashmap. We use modulo to only select every second date starting from the first date, but we are only able to retrieve the first value, but not the second. Then using modulo, we select the third, but not the fourth value.

public void modtagMomsAngivelse() {
    String serviceResponse = "";
    int count = 0;
    for (String name: dates.keySet()) {
        if((count%2) == 0) {
            String startDate = dates.get(name).toString();
            // ----> String endDate = dates.next().get(name).toString(); <---- NEED SOMETHING LIKE THIS, BUT IS IT POSSIBLE?
            system.out.println(startDate + " | " + endDate)
        }
        count++;
    }
}

Above should output the following:

2021-01-01 | 2021-03-31
2021-04-01 | 2021-06-30
2021-07-01 | 2021-09-30
2021-10-01 | 2021-12-31

How can it be programmed correctly in Java?

Answer

You can’t do this with an enhanced for loop. You have to use an Iterator directly:

Iterator<String> it = dates.keySet().iterator();
while (it.hasNext()) {
  String startDateKey = it.next();
  String endDateKey = it.next();   // Will fail if there isn't another element.

  String startDate = dates.get(startDateKey).toString();
  String endDate = dates.get(endDateKey).toString();

  // ...
}

If dates isn’t large, it may be easier to copy into a List, and then access by index:

List<String> keys = new ArrayList<>(dates.keySet());
for (int k = 0; k < keys.size(); k += 2) {
  String startDateKey = keys.get(k);
  String endDateKey = keys.get(k + 1);   // Will fail if there isn't another element. Can change condition to `k + 1 < keys.size()`, if you want to miss off an unpaired element.

  // ...
}

Really, a (LinkedHash)Map isn’t an ideal structure to be holding together pairs of keys with semantic meaning. It would be better to store those keys together, as a single key object.