[java] how to get the one entry from hashmap without iterating

Is there a elegant way of obtaining only one Entry<K,V> from HashMap, without iterating, if key is not known.

As order of entry of entry is not important, can we say something like

hashMapObject.get(zeroth_index);

Although I am aware that there exist no such get by index method.

If I tried approach mentioned below, it would still have to get all the entry set of the hashmap.

for(Map.Entry<String, String> entry : MapObj.entrySet()) {
    return entry;
}

Suggestions are welcome.

EDIT: Please suggest any other Data Structure to suffice requirement.

This question is related to java collections

The answer is


What do you mean with "without iterating"?

You can use map.entrySet().iterator().next() and you wouldn't iterate through map (in the meaning of "touching each object"). You can't get hold of an Entry<K, V> without using an iterator though. The Javadoc of Map.Entry says:

The Map.entrySet method returns a collection-view of the map, whose elements are of this class. The only way to obtain a reference to a map entry is from the iterator of this collection-view. These Map.Entry objects are valid only for the duration of the iteration.

Can you explain in more detail, what you are trying to accomplish? If you want to handle objects first, that match a specific criterion (like "have a particular key") and fall back to the remaining objects otherwise, then look at a PriorityQueue. It will order your objects based on natural order or a custom-defined Comparator that you provide.


for getting Hash map first element you can use bellow code in kotlin:

val data : Float = hashMap?.get(hashMap?.keys?.first())

This would get a single entry from the map, which about as close as one can get, given 'first' doesn't really apply.

import java.util.*;

public class Friday {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<String, Integer>();

        map.put("code", 10);
        map.put("to", 11);
        map.put("joy", 12);

        if (! map.isEmpty()) {
            Map.Entry<String, Integer> entry = map.entrySet().iterator().next();
            System.out.println(entry);
        }
    }
}

Why do you want to avoid calling entrySet() it does not generally create an entirely new object with its own context, but instead just provide a facade object. Simply speaking entrySet() is a pretty cheap operation.


Get values, convert it to an array, get array's first element:

map.values().toArray()[0]

W.


If you are using Java 8, it is as simple as findFirst():

Quick example:

Optional<Car> theCarFoundOpt = carMap.values().stream().findFirst();

if(theCarFoundOpt.isPresent()) {
    return theCarFoundOpt.get().startEngine();
}

import java.util.*;

public class Friday {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<String, Integer>();

        map.put("code", 10);
        map.put("to", 11);
        map.put("joy", 12);

        if (! map.isEmpty()) {
            Map.Entry<String, Integer> entry = map.entrySet().iterator().next();
            System.out.println(entry);
        }
    }
}

This approach doesn't work because you used HashMap. I assume using LinkedHashMap will be right solution in this case.


If you really want the API you suggested, you could subclass HashMap and keep track of the keys in a List for example. Don't see the point in this really, but it gives you what you want. If you explain the intended use case, maybe we can come up with a better solution.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public class IndexedMap extends HashMap {

    private List<Object> keyIndex;

    public IndexedMap() {
        keyIndex = new ArrayList<Object>();
    }

    /**
     * Returns the key at the specified position in this Map's keyIndex.
     * 
     * @param index
     *            index of the element to return
     * @return the element at the specified position in this list
     * @throws IndexOutOfBoundsException
     *             if the index is out of range (index < 0 || index >= size())
     */
    public Object get(int index) {
        return keyIndex.get(index);
    }

    @Override
    public Object put(Object key, Object value) {

        addKeyToIndex(key);
        return super.put(key, value);
    }

    @Override
    public void putAll(Map source) {

        for (Object key : source.keySet()) {
            addKeyToIndex(key);
        }
        super.putAll(source);
    }

    private void addKeyToIndex(Object key) {

        if (!keyIndex.contains(key)) {
            keyIndex.add(key);
        }
    }

    @Override
    public Object remove(Object key) {

        keyIndex.remove(key);
        return super.remove(key);
    }
}

EDIT: I deliberately did not delve into the generics side of this...


map<string,string>m;
auto it=m.begin();//get iterator to the first element of map(m)
return m->first;//return first string(1st string in map<string,string>m)
//Incase you want the second string 
//you can use return m->second(2st string in map<string,string>m)
//if you want to iterate the whole map you can use loop
for(auto it:m)//m is a map
   cout<<it->first<<endl;

I got the answer:(It's simple )

Take a ArrayList then cast it and find the size of the arraylist. Here it is :

    ArrayList count = new ArrayList();
    count=(ArrayList) maptabcolname.get("k1"); //here "k1" is Key
    System.out.println("number of elements="+count.size());

It will show the size. (Give suggestion). It's working.


Following your EDIT, here's my suggestion :

If you have only one entry, you might replace the Map by a dual object. Depending on the types, and your preferences:

  • an array (of 2 values, key and value)
  • a simple object with two properties

Stumbled here looking for the same thing...I then remembered the Iterables class from the Guava library.

To get the "first" element: Iterables.getFirst( someMap.values(), null );.
Essentially does the same as Map.values().iterator().next(), but also allows you to specify a default (in this case null) should there be nothing in the Map.

Iterables.getLast( someMap.values(), null ); returns the last element in the Map.

Iterables.get( someMap.values(), 7, null ); returns the 7th element in the Map if exists, otherwise a default value (in this case null).

Remember though that HashMaps are not ordered...so don't expect Iterables.getFirst to return the first item you tossed in there...likewise with Iterables.getLast. Perhaps useful to get a mapped value though.

It may not be worth adding the Guava library for just that, but if you happen to be using some of the other cool utilities within that library...


I guess the iterator may be the simplest solution.

return hashMapObject.entrySet().iterator().next();

Another solution (not pretty):

return new ArrayList(hashMapObject.entrySet()).get(0);

Or yet (not better):

return hashMapObject.entrySet().toArray()[0];

Maps are not ordered, so there is no such thing as 'the first entry', and that's also why there is no get-by-index method on Map (or HashMap).

You could do this:

Map<String, String> map = ...;  // wherever you get this from

// Get the first entry that the iterator returns
Map.Entry<String, String> entry = map.entrySet().iterator().next();

(Note: Checking for an empty map omitted).

Your code doesn't get all the entries in the map, it returns immediately (and breaks out of the loop) with the first entry that's found.

To print the key and value of this first element:

System.out.println("Key: "+entry.getKey()+", Value: "+entry.getValue());

Note: Calling iterator() does not mean that you are iterating over the whole map.