I am working with many LinkedHashMap
that are either LinkedHashMap<Long, Long>
, LinkedHashMap<Long, Double>
or LinkedHashMap<Long, Integer>
.
My objective is to find or create a method that will return a List<Long>
with all the keys in the above LinkedHashMap<Long,...>
in the same order. The ordering is important which is why I don't think I can use myMap.keySet()
which is a Set<Long>
. Also, I have many other methods that accept only List<Long>
as the input so I would like the desired method to return in that object type so I can continue to use these methods.
Writing a method to return this for, e.g., a LinkedHashMap<Long, Long>
is easy enough:
private static List<Long> getLongKeys(LinkedHashMap<Long, Long> target) {
List<Long> keys = new ArrayList<Long>();
for(Map.Entry<Long, Long> t : target.entrySet()) {
keys.add(t.getKey());
}
return keys;
}
However, then I need to write almost identical methods except for LinkedHashMap<Long, Double>
and LinkedHashMap<Long, Integer>
.
Is there any way I can generalize the method that I pasted to accept all three types: LinkedHashMap<Long, Long>
, LinkedHashMap<Long, Double>
or LinkedHashMap<Long, Integer>
?