java迭代器原理及迭代map的四種方式
迭代器原理:
什么是迭代器,使用迭代器的好處?
迭代器就是用來遍歷集合中對象的東西,也就是說,對于集合,我們不像對原始數(shù)組那樣通過直接訪問元素來迭代的,而是通過迭代器來遍歷對象。這么做的好處是將對于集合類型的遍歷行為與被遍歷集合對象分離,這樣以來,就不需要關(guān)心該集合類型的具體實現(xiàn)是怎么樣的。只要獲取這個集合對象的迭代器便可以遍歷這個集合中的對象。而像遍歷對象順序以及怎么訪問對象元素這些細節(jié),全部由它自己的迭代器來處理。
迭代器怎么實現(xiàn)的?
首先集合要先實現(xiàn)iterable接口來表示此對象是可以進行迭代的。而實現(xiàn)iterable接口的對象實現(xiàn)了iterator方法,這個方法返回了一個Iterator對象。一個迭代器對象需要Iterator接口中的方法:hasNext(),next(),remove()。remove()方法會刪除最近一次調(diào)用的元素,如果remove()之前沒有調(diào)用next()的話直接調(diào)用remove()會產(chǎn)生報錯信息(IllegalStateException)。我們在進行對集合對象迭代的時候,next()會返回當(dāng)前對象第一個對象并返回,然后next會指向下一個元素,hasNext方法就是看這個指針后面還有沒有元素了。
迭代器的陷阱?
使用for迭代的時候不可以使用集合進行remove操作。這時候需要使用迭代器進行迭代,然后使用迭代器中的remove方法進行刪除。
為什么會產(chǎn)生這樣的錯誤?
remove()方法在刪除元素的時候,還會修改一個修改次數(shù)的標(biāo)志位modCount,如果iterator的expectedModCount與modCount的大小不相等時,會拋出一個ConcurrentModificationException異常。modCount的目的主要是為了防止當(dāng)前對象迭代過程中存在其他線程對當(dāng)前對象的修改。
// iterable接口源代碼
public interface Iterable<T> {
/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
Iterator<T> iterator();
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
default Spliterator<T> spliterator() {
return Spliterators.spliteratorUnknownSize(iterator(), 0);
}
}
// iterator接口源代碼
public interface Iterator<E> {
/**
* 如果迭代擁有更多元素,那么返回true
*/
boolean hasNext();
/**
* 返回iteration中的下一個元素
*/
E next();
/**
* 如果刪除一個集合中的元素沒有調(diào)用這個方法,二十直接中集合中刪除,那么這個迭代器的行為沒有被指定
*/
default void remove() {
throw new UnsupportedOperationException("remove");
}
/**
* 遍歷集合中的剩余元素(如果之前調(diào)用了兩次next()那么只會遍歷集合中剩余元素
* 使用案例:
* Iterator<Integer> it = map.keySet().iterator();
it.next();
it.next();
it.forEachRemaining(System.out::println);
*/
default void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while (hasNext())
action.accept(next());
}
}
HashMap中實現(xiàn)迭代的核心代碼:
final Node<K,V> nextNode() {
Node<K,V>[] t;
Node<K,V> e = next; // 對象屬性中的next是下一個值
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
if (e == null)
throw new NoSuchElementException();
// next = e.next 如果e.next為null,那么繼續(xù)找數(shù)組下一個不為null的值
if ((next = (current = e).next) == null && (t = table) != null) {
do {} while (index < t.length && (next = t[index++]) == null);
}
return e;
}
public final void remove() {
Node<K,V> p = current;
if (p == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
current = null;
K key = p.key;
removeNode(hash(key), key, null, false, false);
expectedModCount = modCount;
遍歷map的四種方式
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.HashMap;
public class MapTest {
public static void main(String[] args) {
HashMap<Integer,Integer> map = new HashMap();
map.put(0,1);
map.put(2,2);
map.put(1,2);
map.put(4,5);
map.put(3,4);
// 遍歷hashmap entry foreach
Set<Map.Entry<Integer,Integer>> ent = map.entrySet();
for(Map.Entry<Integer,Integer> entry:ent){
System.out.println(entry.getKey()+" : "+entry.getValue());
//map.remove(0);
}
System.out.println();
// 通過keySet或者values()遍歷
Set<Integer> set = map.keySet();
for(Integer key:set){
System.out.println(key+" -- "+map.get(key));
}
Collection<Integer> set1 = map.values();
for(Integer val:set1){
System.out.println(val);
}
System.out.println();
// iterator原理是什么 通過iterator遍歷map
Iterator<Map.Entry<Integer,Integer>> iter = map.entrySet().iterator();
while(iter.hasNext()){
Map.Entry entry = iter.next();
System.out.println(entry.getKey()+" : "+entry.getValue());
iter.remove();
}
System.out.println();
Iterator<Integer> keys = map.keySet().iterator();
while(keys.hasNext()){
int k = keys.next();
System.out.println(k+" -- "+ map.get(k));
}
}
}
參考鏈接:https://blog.csdn.net/fuzhongmin05/article/details/72460658
到此這篇關(guān)于java迭代器原理及迭代map的四種方式的文章就介紹到這了,更多相關(guān)java迭代map內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
kafka消費者kafka-console-consumer接收不到數(shù)據(jù)的解決
這篇文章主要介紹了kafka消費者kafka-console-consumer接收不到數(shù)據(jù)的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
使用MyBatis攔截器實現(xiàn)sql查詢權(quán)限動態(tài)修改代碼實例
這篇文章主要介紹了使用MyBatis攔截器實現(xiàn)sql查詢權(quán)限動態(tài)修改代碼實例,為了不耦合,現(xiàn)在的方案是在需要鑒權(quán)的Mybatis?Mapper方法上增加一個注解,在運行過程中判斷該注解存在即對sql進行修改,需要的朋友可以參考下2023-08-08
java中的數(shù)學(xué)計算函數(shù)的總結(jié)
這篇文章主要介紹了java中的數(shù)學(xué)計算函數(shù)的總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-07-07

