OneBite.Dev - Coding blog in a bite size

loop object in java

Code snippet on how to loop object in java

  for(String key : object.keySet()) {
    Object value = object.get(key);
    System.out.println("Key is: " + key + " Value is: " + value);
}

This code loops through an object in java. It starts off by declaring a “for loop”. This loop iterates through our object’s key Set. It then declares a local variable and assigns it the value associated with the current key. Next, it prints out the current key and associated value. This loop continues until it’s gone through the entire set of keys within our object. This allows us to loop through and print out the key and value of each item held inside of our object.

java