Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get element position from Java Map

Tags:

java

I have this Java Map:

Can you tell me how I can get the 6-th element of the Map?

private static final Map<String, Users> cache = new HashMap<>();

is this possible? Or I have to use another Java collection?

like image 290
Peter Penzov Avatar asked Nov 27 '13 10:11

Peter Penzov


3 Answers

Though a bit late to answer. But the option is to use LinkedHashMap: this map preserves the order according to insertion of elements, as everyone has suggested. However, As a warning, it has a constructor LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) which will create a linked hash map whose order of iteration is the order in which its entries were last accessed. Don't use this constructor for this case.

However, if I needed such functionality, i would extend it and implement my necessary function to re-use them in OOP way.

class MyLinkedMap<K, V> extends LinkedHashMap<K, V>
{

    public V getValue(int i)
    {

       Map.Entry<K, V>entry = this.getEntry(i);
       if(entry == null) return null;

       return entry.getValue();
    }

    public Map.Entry<K, V> getEntry(int i)
    {
        // check if negetive index provided
        Set<Map.Entry<K,V>>entries = entrySet();
        int j = 0;

        for(Map.Entry<K, V>entry : entries)
            if(j++ == i)return entry;

        return null;

    }

}

Now i can instantiate it and can get a entry and value either way i want:

MyLinkedMap<String, Integer>map = new MyLinkedMap<>();
map.put("a first", 1);
map.put("a second", 2);
map.put("a third", 3);

System.out.println(map.getValue(2));
System.out.println(map.getEntry(1)); 

Output:

3
a second=2
like image 198
Sage Avatar answered Oct 03 '22 17:10

Sage


HashMap doesn't grantee the order. If you concern about order you should use LinkedHashMap

Map<String, Users> orderedMap=new LinkedHashMap<>();

Now when you put an element it will keep the order what you put.

If you want to get 6th element, now you can do it since you have your elements in order.

 orderedMap.values().toArray()[5]// will give you 6th value in the map. 

Example

 Map<String, String> orderedMap=new LinkedHashMap<>();
 orderedMap.put("a","a");
 orderedMap.put("b","b");
 System.out.println(orderedMap.values().toArray()[1]);  // you will get b(value)
 System.out.println(orderedMap.keySet().toArray()[1]);  // you will get b(key)
    }
like image 43
Ruchira Gayan Ranaweera Avatar answered Oct 03 '22 16:10

Ruchira Gayan Ranaweera


A HashMap does not maintain the order of the elements inserted in it. You can used a LinkedHashMap instead which maintains the order of the elements inserted in it.

Though you need to note that even a LinkedHashMap has no such method which would give the element at a particular index. You will have to manually iterate through the entries and extract the element at the 6th iteration.

like image 7
Rahul Avatar answered Oct 03 '22 15:10

Rahul