1

I have an ArrayList HashMap like the one below.

    ArrayList<HashMap<String, String>> mArrType = new ArrayList<>();

with the following values added into it

    HashMap<String, String> map;

    map = new HashMap<String, String>();
    map.put("type", "TRIMMER");
    map.put("request", "5");
    map.put("actual", "0");
    mArrType.add(map);

    map = new HashMap<String, String>();
    map.put("type", "HAND ROUTER");
    map.put("request", "6");
    map.put("actual", "0");
    mArrType.add(map);

    map = new HashMap<String, String>();
    map.put("type", "AIR COMPRESSOR");
    map.put("request", "6");
    map.put("actual", "0");
    mArrType.add(map);

Question is how can i get the position of a hashmap from arraylist. eg : hashmap with 'type' trimmer has a position 0 in arraylist, I want to retrieve the position value "0"

1
  • Of course, what you really should be doing here is creating a class Equipment. Commented Oct 7, 2016 at 5:17

3 Answers 3

2

I'll write a small util method

private static int getTrimmerTypeMapPosition(ArrayList<HashMap<String, String>> mArrType) {
        for (int i = 0; i < mArrType.size(); i++) {
            HashMap<String, String> mp = mArrType.get(i);
            if (mp.get("type").equals("TRIMMER")) {
                return i;
            }    
        }    
        return -1;    
    }

To make this method very generic, have "type" and "TRIMMER" as method params, so that you can just pass any key and value pairs to check with.

Sign up to request clarification or add additional context in comments.

2 Comments

I suppose better to check like this "TRIMMER".equals(mp.get("type")) to prevent exception in case when mp.get("type") returns null.
@Roman_D I'll leave that decisions to OP as he will check what if type is not present in map at all.:)
0

That's not efficiently possible with your data structure. You can either store the own position in each HashMap or loop through all entries and search for the one with the type you are looking for.

You can, of course, define another HashMap<String, Integer> which maps all your type strings to the corresponding ArrayList index.

3 Comments

I dont think OP need extra map. Just iterate over list and look up for type ?
Yes, but if he has very many entries in the ArrayList it's more efficient to hash it.
You have a point, I've tried store position value in each hashmap and it works. But I marked suresh answer as the best, cheers.
0

Others answer is also correct, but you can do this thing using Java8 also.

E.g.:

int index = IntStream.range(0, mArrType.size()).
                filter(i -> mArrType.get(i).get("type").equals("TRIMMER"))
                .findFirst().getAsInt();

2 Comments

Not just map, needed index of map which have a "type" value "Trimmer"
Yes he is, thank for pointing this thing. i have updated my answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.