HashMap does not support key insertion order.
LinkedHashMap should be used because it provides a predictable iteration order, which is usually the order in which keys were inserted into the map (insertion order).
You can use the MapEntry method to iterate over the LinkedHashMap link. So here is what you need to do in the code. First, change the bank card from HashMap to LinkedHashMap:
public static Map<String, Inventory> banks = new LinkedHashMap<String, Inventory>();
And then just repeat it like this:
for (Map.Entry<String, Inventory> entry : banks.entrySet()) { InventoryManager.saveToYaml(entry.getValue(), size, entry.getKey()); }
If you only need the first LinkedHashMap element, you can do this:
banks.entrySet().iterator().next();
Juned ahsan
source share