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?
1 Answer 1
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.
-
This works perfectly. Although, my question was slightly misunderstood, most likely, due to my bad formulation. What i wanted was to iterate through values, and not keys. Therefore choosing your last example, it was solved by following: "List<String> keys = new ArrayList<>(dates.values());". Thank you very much for your answer!Buster3650– Buster36502021年09月02日 11:00:05 +00:00Commented Sep 2, 2021 at 11:00