Skip to content

Instantly share code, notes, and snippets.

@h-hub
Created September 10, 2021 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h-hub/8ba30fe12ac84d52f113fe6e5e260fc9 to your computer and use it in GitHub Desktop.
Save h-hub/8ba30fe12ac84d52f113fe6e5e260fc9 to your computer and use it in GitHub Desktop.
Iterator example
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class ItrExample {
public static void main(String[] args) {
List<String> list2 = List.of("item11","item22","item33"); //create a list
Iterator<String> itr = list2.iterator(); // create list iterator
while(itr.hasNext()){
System.out.println(itr.next());
}
Map<String,String> map = new HashMap<String,String>(); //create a map
map.put("item1", "item1-content");
map.put("item2", "item2-content");
map.put("item3", "item3-content");
Iterator entrySIterator = map.values().iterator(); //create map values iterator
while(entrySIterator.hasNext()){
System.out.println(entrySIterator.next());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment