Java 集合接口的 iterator() 方法返回此集合中元素的迭代器。
用法
public Iterator <E> iterator()
参数
NA
返回
iterator() 方法返回一个遍历此集合中元素的迭代器。
例子1
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
public class JavaCollectionIteratorExample1 {
static int i = 1;
public static void main(String[] args) {
Collection<String> collection = new ConcurrentLinkedQueue<String>();
collection.add("Ram");
collection.add("Sham");
collection.add("Mira");
collection.add("Rajesh");
Iterator<String> iterator = collection.iterator();
//Returns an iterator over the elements
while (iterator.hasNext()) {
System.out.println(i++ + "." + iterator.next());
}
}
}
输出:
1.Ram 2.Sham 3.Mira 4.Rajesh
例子2
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
public class JavaCollectionIteratorExample2 {
public static void main(String[] args) {
Integer[] val = new Integer[2];
Collection<Integer> collection = new ConcurrentLinkedQueue<Integer>();
collection.add(71018);
collection.add(8000);
collection.add(1178);
collection.add(1190);
//Returns an iterator over the elements
Iterator<Integer> iterator = collection.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
val[0] = Collections.max(collection);
val[1] = Collections.min(collection);
}
System.out.println("Max number = "+ val[0]);
System.out.println("Min number = "+ val[1]);
}
}
输出:
71018 8000 1178 1190 Max number = 71018 Min number = 1178
例子3
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingDeque;
public class JavaCollectionIteratorExample3 {
public static void main(String[] args) {
Collection<Character> collection = new LinkedBlockingDeque<Character>();
for (char c = 'a'; c <= 'z'; c++) {
collection.add(c);
}
System.out.print("Characters:");
System.out.print("[");
Iterator<Character> iterator = collection.iterator();
//will return the iterator for all characters
while (iterator.hasNext()) {
System.out.print(iterator.next()+" ,");
}
System.out.println("]");
}
}
输出:
Characters:[a ,b ,c ,d ,e ,f ,g ,h ,i ,j ,k ,l ,m ,n ,o ,p ,q ,r ,s ,t ,u ,v ,w ,x ,y ,z ,]
相关用法
- Java Collection isEmpty()用法及代码示例
- Java Collection retainAll()用法及代码示例
- Java Collection toArray()用法及代码示例
- Java Collection addAll()用法及代码示例
- Java Collection size()用法及代码示例
- Java Collection add()用法及代码示例
- Java Collection removeAll()用法及代码示例
- Java Collection remove()用法及代码示例
- Java Collection equals()用法及代码示例
- Java Collection hashCode()用法及代码示例
- Java Collection contains()用法及代码示例
- Java Collection spliterator()用法及代码示例
- Java Collection containsAll()用法及代码示例
- Java Collection removeIf()用法及代码示例
- Java Collection clear()用法及代码示例
- Java Collections synchronizedSortedSet()用法及代码示例
- Java Collections checkedQueue()用法及代码示例
- Java Collections unmodifiableNavigableSet()用法及代码示例
- Java Collections checkedSet()用法及代码示例
- Java Collections copy()用法及代码示例
注:本文由纯净天空筛选整理自 Java Collection iterator() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。