Skip to content

Commit 1209164

Browse files
committed
Add MapGenerator#mapOf(List)
1 parent 5829fb7 commit 1209164

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/main/java/xyz/srnyx/javautilities/MapGenerator.java

+18
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ public <T, G> Map<T, G> mapOf() {
5252
return (Map<T, G>) supplier.get();
5353
}
5454

55+
/**
56+
* Generates a map from a list of keys and values (alternating)
57+
* <br><b>This method is not type-safe and may throw {@link ClassCastException}!</b>
58+
*
59+
* @param keysValues the list of keys and values (key1, value1, key2, value2, ...)
60+
*
61+
* @return the generated map
62+
*
63+
* @param <T> the type of the keys
64+
* @param <G> the type of the values
65+
*/
66+
@NotNull
67+
public <T, G> Map<T, G> mapOf(@NotNull List<?> keysValues) {
68+
final Map<T, G> map = mapOf();
69+
for (int i = 0; i < keysValues.size(); i += 2) map.put((T) keysValues.get(i), (G) keysValues.get(i + 1));
70+
return map;
71+
}
72+
5573
/**
5674
* Generates a map from a list of keys and a list of values
5775
*

0 commit comments

Comments
 (0)