Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through a Map in java?

Tags:

I need to iterate through a BucketMap and get all keys but how do I get to something like buckets[i].next.next.next.key for instance without doing it manually as I tried here:

public String[] getAllKeys() {
    //index of string array "allkeys"
    int j = 0;
    String allkeys[] = new String[8];
    //iterates through the bucketmap
    for (int i = 0; i < buckets.length; i++) {
        //checks wether bucket has a key and value
        if (buckets[i] != null) {
            //adds key to allkeys
            allkeys[j] = buckets[i].key;
            // counts up the allkeys index after adding key
            j++;
            //checks wether next has a key and value
            if (buckets[i].next != null) {
                //adds key to allkeys
                allkeys[j] = buckets[i].next.key;
                j++;
            }
        }
    }
    return allkeys;
}

Also how can I initialize the String[] allkeys using the version of j we get after the iteration is done as the index?

like image 820
NoIdea Avatar asked Mar 25 '17 10:03

NoIdea


People also ask

How many ways can you iterate a map in Java?

There are generally five ways of iterating over a Map in Java.

What is entrySet in map Java?

entrySet() method in Java is used to create a set out of the same elements contained in the hash map. It basically returns a set view of the hash map or we can create a new set and store the map elements into them.

How do I iterate through a map in JavaScript?

Iterate through a Map using JavaScript # Use the forEach() method to iterate over a Map object. The forEach method takes a function that gets invoked for each key/value pair in the Map , in insertion order. The function gets passed the value, key and the Map object on each iteration.


2 Answers

For basic utilisation, the HashMap is the best, I've put how to iterate over it, easier than using an iterator :

public static void main (String[] args) {
    //a map with key type : String, value type : String
    Map<String,String> mp = new HashMap<String,String>();
    mp.put("John","Math");    mp.put("Jack","Math");    map.put("Jeff","History");

    //3 differents ways to iterate over the map
    for (String key : mp.keySet()){
        //iterate over keys
        System.out.println(key+" "+mp.get(key));
    }

    for (String value : mp.values()){
        //iterate over values
        System.out.println(value);
    }

    for (Entry<String,String> pair : mp.entrySet()){
        //iterate over the pairs
        System.out.println(pair.getKey()+" "+pair.getValue());
    }
}

A quick explanation :

for (String name : mp.keySet()){
        //Do Something
}

means : "For all string from the keys of the map, we'll do something, and at each iteration we will call the key 'name' (it can be whatever you want, it's a variable)


Here we go :

public String[] getAllKeys(){ 
    int i = 0;
    String allkeys[] = new String[buckets.length];
    KeyValue val = buckets[i];

    //Look at the first one          
    if(val != null) {             
        allkeys[i] = val.key; 
        i++;
    }

    //Iterate until there is no next
    while(val.next != null){
        allkeys[i] = val.next.key;
        val = val.next;
        i++;
    }

    return allkeys;
}
like image 142
azro Avatar answered Sep 27 '22 19:09

azro


See if this helps,

  HashMap< String, String> map = new HashMap<>();
  Set<String> keySet = map.keySet();
  Iterator<String> iterator = keySet.iterator();
  while(iterator.hasNext())
  {
    //iterate over keys
  }

//or iterate over entryset 
Iterator<Entry<String, String>> iterator2 = map.entrySet().iterator();

while(iterator2.hasNext())
{
    Entry<String, String> next = iterator2.next();
    //get key
    next.getKey();
    //get value
    next.getValue();
}
like image 20
DNAj Avatar answered Sep 27 '22 18:09

DNAj