uriel
uriel

Reputation: 1248

parsing on HashMap in Java

I've simple question.

I set:

HashMap<A, B> myMap = new HashMap<A, B>();

...
myMap.put(...)
...

Now I want to Loop through myMap and get all the keys (of type A). how can I do that?

I want to get all keys from myMap by loop, and send them to "void myFunction(A param){...}".

Upvotes: 3

Views: 27596

Answers (8)

Younes Meridji
Younes Meridji

Reputation: 309

If you will not remove elements from your map, you can use this:

for(A k:myMap.keySet()) {
    //the correspending value is myMap.get(k))
}

But if you want to remove elements from your map you have to use an iterator. Here is an exemple to show you:

public class TutoMap {

    public static void main(String[] args) {

        //Create a map
        Map<Integer, String> map = new HashMap<>();

        //add some elements to the map
        map.put(0, "a");
        map.put(5, "b");
        map.put(3, "c");
        map.put(4, "e");
        map.put(10, "d");

        //get the iterator of the Entry set
        Iterator iter = map.entrySet().iterator();

        //try to remove element of key = 4
        while (iter.hasNext()) {
            Map.Entry<Integer, String> ele = (Map.Entry<Integer, String>) iter.next();
            if (ele.getKey() == 4) {
                iter.remove();
            }
        }
    }
}

Upvotes: 0

oHo
oHo

Reputation: 54561

This is a more generic answer based on question title.

Parsing keys & values using entrySet()

HashMap<A, B> myMap = new HashMap<A, B>();

...
myMap.put(key, value);
...

for (Entry<A, B> e : myMap.entrySet()) {
    A key    = e.getKey();
    B value  = e.getValue();
}

//// or using an iterator:

// retrieve a set of the entries
Set<Entry<A, B>> entries = myMap.entrySet();
// parse the set
Iterator<Entry<A, B>> it = entries.iterator();
while(it.hasNext()) {
    Entry<A, B> e = it.next();
    A key   = e.getKey();
    B value = e.getValue();
}

Parsing keys using keySet()

HashMap<A, B> myMap = new HashMap<A, B>();

...
myMap.put(key, value);
...

for (A key   : myMap.keySet()) {
     B value = myMap.get(key);  //get() is less efficient 
}                               //than above e.getValue()

// for parsing using a Set.iterator see example above 

See more details about entrySet() vs keySet() on question Performance considerations for keySet() and entrySet() of Map.

Parsing values using values()

HashMap<A, B> myMap = new HashMap<A, B>();

...
myMap.put(key, value);
...

for (B value : myMap.values()) {
    ...
}

//// or using an iterator:

// retrieve a collection of the values (type B)
Collection<B> c = myMap.values();   
// parse the collection
Iterator<B> it = c.iterator();
while(it.hasNext())
  B value = it.next();
}

Upvotes: 8

duffymo
duffymo

Reputation: 308763

Here's how you can get the key set:

Set<A> keys = myMap.keySet();

I don't know what "passing on" means. I don't know what "parsing" means for a HashMap, either. Except for getting the keys out of the Map, this question makes no sense whatsoever. Voting to close.

Upvotes: 4

Abhij
Abhij

Reputation: 1272

if u want to get all key value pair from map
use map.keyset() it will return all keys in a set
now u can use iterator to iterate all key values
and use it in map.get(key).

Upvotes: 0

Kris
Kris

Reputation: 5792

You can use Google Guava for filtering your collections. An examples for filtering, ordering etc can be found here

Upvotes: 0

csturtz
csturtz

Reputation: 6580

After passing the map to wherever you're passing it to, the method/class ending up with the map would make the following call to get the set of keys in the map.

Set<A> keys = myMap.keySet();

Upvotes: 1

Kurt Du Bois
Kurt Du Bois

Reputation: 7665

myMap.keySet() ? Not sure what you mean actually.

Upvotes: 1

tartak
tartak

Reputation: 485

map.keySet() will return you the set containing all keys .. from here you can parse the set and get all the keys

Upvotes: 1

Related Questions