Traversing collection data using Java iterators

Source: Internet
Author: User
Keywords Java iterators Traversing collection data
Tags java java iterators iterable interface iterator interface

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<>();

//collection
List.add(1);
List.add(2);
List.add(3);

//Iterator iterator
//1, get an iterator
Iterator iter = list.iterator();
//2, iterating through the loop
//hasNext(): determine if there is a next element
While(iter.hasNext()){
//If it exists, call next to iterate
//Object-->Integer-->int
Int j=(int)iter.next(); //Strengthen Object to int
System.out.println(j);
}
}
 
}

2, use the iterator 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) {
/ / Store the address of the 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"));

/ / Traverse the data
Iterator iter = set.iterator();
While(iter.hasNext()){
Theme theme = (Theme)iter.next();
System.out.println(theme.getId()+theme.getTitle()+theme.getRemark());
}
}
}

3, use iterator 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 keys in a key-value pair form a collection
Set set = map.keySet();
Iterator iter = set.iterator();
While(iter.hasNext()){
System.out.println(iter.next());//Print out the keys in the map (1,2,3)
}


/ / Print out the value
//values a collection of all the values
Collection col = map.values();
/ / Rewritten the toString method
System.out.println(col);//Print out the values of a, b, 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");
/ / Must master
//All keys in a key-value pair are grouped into a set set
Set set=map.keySet();
System.out.println(set);
//values a collection of all the values
Collection col=map.values();
System.out.println(col);





/ / Get all the keys and values
//entrySet can get a collection of all key-value pairs
//The inside is all the data (key-value) stored.
Set<Map.Entry<Integer, String>> entrySet=map.entrySet();
Iterator<Map.Entry<Integer, String>> iter=entrySet.iterator();
While(iter.hasNext()) {
Map.Entry<Integer, String> 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);
}
 
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.