迭代是迭代器模式的一种体现,使得访问一个聚合对象的内容而无需暴露它的内部表示,为遍历不同的聚合结构提供一个统一的接口,使接口与实现隔离。
一、Iterator迭代
集合中迭代可以使用Iterator。Iterator也是Java集合框架中的成员,Iterator主要用于遍历集合中的元素,所以Iterator也称为迭代器。
Iterator中隐藏了各种Collection实现类的底层细节,向应用程序提供便利Collection集合元素的统一编程接口。
Iterator本身不具有承装元素的能力,所以如果需要创建Iterator对象需要有一个被迭代的集合。没有集合的Iterator是没有意义的。
需要说明的是Iterator并不是把集合元素本身传给了迭代变量,而是把集合元素的值传递给了迭代变量,所以修改迭代变量对元素本身是没有影响的。
除Map集合类外,其他的集合类均存在Iterator()方法,用于获取该集合的Iterator变量。
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorNormal {
public static void main(String[] args) {
ArrayList<String> as = new ArrayList<String>();
as.add("one");
as.add("two");
as.add("thi");
as.add("fou");
as.add("fir");
for (Iterator it = as.iterator(); it.hasNext();) {
System.out.println(it.next());
}
}
}
当对Map采用Iterator迭代时,可以将Map中的Key值提取至Set集合中
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
class student implements Comparable<student> {
private String nameString;
private int age;
public student(String string, int i) {
// TODO Auto-generated constructor stub
nameString = string;
age = i;
}
public String getNameString() {
return nameString;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "student [nameString=" + nameString + ", age=" + age
+ ", getNameString()=" + getNameString() + ", getAge()="
+ getAge() + ", getClass()=" + getClass() + ", hashCode()="
+ hashCode() + ", toString()=" + super.toString() + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result
+ ((nameString == null) ? 0 : nameString.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
student other = (student) obj;
if (age != other.age)
return false;
if (nameString == null) {
if (other.nameString != null)
return false;
} else if (!nameString.equals(other.nameString))
return false;
return true;
}
@Override
public int compareTo(student o) {
// TODO Auto-generated method stub
int temp;
temp = age - o.getAge();
return temp == 0 ? nameString.compareTo(o.getNameString()) : temp;
}
}
public class iteratortext {
public static void main(String[] args) {
// TODO Auto-generated method stub
method1();
System.out.println("++++++++++++++++++++++++++++++++++");
method2();
System.out.println("++++++++++++++++++++++++++++++++++");
method3();
System.out.println("++++++++++++++++++++++++++++++++++");
method4();
}
public static void method1() {
Map<String, Integer> studentMap = new HashMap<String, Integer>();
studentMap.put("lisi", 21);
studentMap.put("sdfs", 26);
studentMap.put("fsai", 24);
studentMap.put("hfij", 28);
studentMap.put("mdlj", 29);
studentMap.put("piop", 27);
studentMap.put("cvnb", 25);
Set<String> set = studentMap.keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String name = it.next();
int age = studentMap.get(name);
System.out.println(name + "---" + age);
}
}
public static void method2() {
Map<String, Integer> studentMap = new HashMap<String, Integer>();
studentMap.put("lisi", 21);
studentMap.put("sdfs", 26);
studentMap.put("fsai", 24);
studentMap.put("hfij", 28);
studentMap.put("mdlj", 29);
studentMap.put("piop", 27);
studentMap.put("cvnb", 25);
Set<Entry<String, Integer>> se = studentMap.entrySet();
Iterator<Entry<String, Integer>> it = se.iterator();
while (it.hasNext()) {
Entry<String, Integer> shEntry = it.next();
String string = shEntry.getKey();
Integer integer = shEntry.getValue();
System.out.println(string + "----------" + integer);
}
}
public static void method3() {
Map<String, Integer> studentMap = new HashMap<String, Integer>();
studentMap.put("lisi", 21);
studentMap.put("sdfs", 26);
studentMap.put("fsai", 24);
studentMap.put("hfij", 28);
studentMap.put("mdlj", 29);
studentMap.put("piop", 27);
studentMap.put("cvnb", 25);
Collection<Integer> value = studentMap.values();
Iterator<Integer> it = value.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
public static void method4() {
TreeMap<student, String> studentMap = new TreeMap<student, String>();
studentMap.put(new student("lisi", 21), "北京");
studentMap.put(new student("sdfs", 26), "天津");
studentMap.put(new student("fsai", 24), "广州");
studentMap.put(new student("hfij", 28), "深圳");
studentMap.put(new student("mdlj", 29), "上海");
studentMap.put(new student("piop", 27), "青岛");
studentMap.put(new student("cvnb", 25), "大连");
Set<Entry<student, String>> set = studentMap.entrySet();
Iterator<Entry<student, String>> it = set.iterator();
while (it.hasNext()) {
Entry<student, String> temp = it.next();
student student = temp.getKey();
String whereString = temp.getValue();
System.out.println(student.getNameString() + "--"
+ student.getAge() + "--" + whereString);
}
}
}
二、foreach迭代
Java5之后允许使用foreach进行迭代,这样的迭代更加简洁
import java.util.ArrayList;
public class ForeachIte {
public static void main(String[] args) {
ArrayList<String> as = new ArrayList<String>();
as.add("one");
as.add("two");
as.add("thi");
as.add("fou");
as.add("fir");
for ( String key : as) {
System.out.println(key);
}
}
}