Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Java uses the Iterator iterator to traverse collection data

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "Java how to use Iterator iterator to traverse collection data", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Java how to use Iterator iterator to traverse collection data" this article.

The details are as follows:

1. Use an iterator to traverse the ArrayList collection

Package com.jredu.oopch07;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class Ch05 {public static void main (String [] args) {/ / TODO Auto-generated method stub List list = new ArrayList (); / / set list.add (1); list.add (2); list.add (3); / / Iterator iterator / / 1, get iterator Iterator iter = list.iterator () / / 2. Iterate through / / hasNext (): determine whether there is a next element while (iter.hasNext ()) {/ / if it exists, then call next to iterate / / Object-- > Integer-- > int int j = (int) iter.next (); / / convert the object type to int System.out.println (j);}

2. Use iterators to traverse the Set collection

Package com.jredu.oopch08;import java.util.HashSet;import java.util.Iterator;import java.util.Set;public class Ch01 {public static void main (String [] args) {/ / address for storing data Set set = new HashSet (); / / storing data set.add (new Theme (1, "title 1", "introduction 1"); set.add (new Theme (2, "title 2", "introduction 1")); / / traversal data Iterator iter = set.iterator () While (iter.hasNext ()) {Theme theme = (Theme) iter.next (); System.out.println (theme.getId () + theme.getTitle () + theme.getRemark ());}}

3. Use iterators to traverse the Map collection

Package com.jredu.oopch08;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class Ch03 {public static void main (String [] args) {/ / TODO Auto-generated method stub Map map = new HashMap (); map.put (1, "a"); map.put (2, "b"); map.put (3, "c") / / all the keys in the key-value pair form a set Set set = map.keySet (); Iterator iter = set.iterator (); while (iter.hasNext ()) {System.out.println (iter.next ()); / / print out a set consisting of all the values in map} / / print out the value / / values all the values Collection col = map.values (); / / rewrite the toString method System.out.println (col) / / print out the value of a _ c}}

Package com.jredu.oopch08;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class Ch04 {public static void main (String [] args) {Map map=new HashMap (); map.put (1, "a"); map.put (2, "b"); map.put (3, "c") / / you must master / / all the keys in key-value pairs to form a set collection Set set=map.keySet (); System.out.println (set); / / values a set of all values Collection col=map.values (); System.out.println (col); / / get all keys and values / / entrySet can get a set of all key-value pairs / / what is stored in it is all the data (key-value) Set entrySet=map.entrySet () Iterator iter=entrySet.iterator (); while (iter.hasNext ()) {Map.Entry entry=iter.next (); System.out.println ("key:" + entry.getKey ()); System.out.println ("value:" + entry.getValue ());} / / Iterator iter=col.iterator (); / / while (iter.hasNext ()) {/ / System.out.println (iter.next ()); / /} / / Iterator iter=set.iterator () / while (iter.hasNext ()) {/ / System.out.println (iter.next ()); / /} / / System.out.println (map);}}

That's all of the article "how Java uses the Iterator iterator to traverse collection data". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report