Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to extract all keys from LinkedHashMap into a List

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>?

like image 521
user2763361 Avatar asked Sep 21 '13 06:09

user2763361


People also ask

How get all keys from LinkedHashMap?

The task is to print all the Keys present in our LinkedHashMap in java. We have to iterate through each Key in our LinkedHashMap and print It. Method 1: Use for-each loop to iterate through LinkedHashMap. For each iteration, we print the respective key using getKey() method.

How do I cast a LinkedHashMap to a list?

To convert all values of the LinkedHashMap to a List using the values() method. The values() method of the LinkedHashMap class returns a Collection view of all the values contained in the map object. You can then use this collection to convert it to a List object.


2 Answers

The ordering is important which is why I don't think I can use myMap.keySet() which is a Set

The Map#keySet() method for LinkedHashMap will return the set in insertion order. Here's a quote from Map documentation:

The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

So, you don't need to write a separate method for that. Methods like keySet() and entrySet() will return the entries in the insertion order only.


Well, if you really want a List<Keys>, then you can directly do:

List<Long> keys = new ArrayList<>(target.keySet());

.. wherever you want a List. You don't need the method at all.

like image 57
Rohit Jain Avatar answered Nov 12 '22 20:11

Rohit Jain


LinkedHashMap's consistent ordering applies to keys, values and entries.

You should be just fine doing:

ArrayList<Long> keys = new ArrayList<>(target.keySet());

If you need more assurance, take a look at the source code for LinkedHashMap. The important part:

private class KeyIterator extends LinkedHashIterator<K> {
    public K next() { return nextEntry().getKey(); }
}

private class ValueIterator extends LinkedHashIterator<V> {
    public V next() { return nextEntry().value; }
}

private class EntryIterator extends LinkedHashIterator<Map.Entry<K,V>> {
    public Map.Entry<K,V> next() { return nextEntry(); }
}

// These Overrides alter the behavior of superclass view iterator() methods
Iterator<K> newKeyIterator()   { return new KeyIterator();   }
Iterator<V> newValueIterator() { return new ValueIterator(); }
Iterator<Map.Entry<K,V>> newEntryIterator() { return new EntryIterator(); }

So the iterators for the keys, values and entries all come from the same source (nextEntry()) which uses the linked list for iteration order.

like image 25
prunge Avatar answered Nov 12 '22 19:11

prunge