private class Itr implements Iterator public boolean hasNext() { return cursor != size(); } public Object next() { checkForComodification(); try { Object next = get(cursor); lastRet = cursor++; return next; } catch(IndexOutOfBoundsException e) { checkForComodification(); throw new NoSuchElementException(); } }
楼上说的,我不是很明白。这样我发段代码 下面这段代码,使用的是 AbstractMap 的toString()方法,其中需要调用 entrySet() 方法中对象的iterator()。我想了解的是,其中,hasNext()、next()方法的具体实现是在哪里?public class CountingMapData extends AbstractMap<Integer,String> { private int size; private static String[] chars = "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" .split(" "); public CountingMapData(int size) { if(size < 0) this.size = 0; this.size = size; } private static class Entry implements Map.Entry<Integer,String> { int index; Entry(int index) { this.index = index; } public boolean equals(Object o) { return Integer.valueOf(index).equals(o); } public Integer getKey() { return index; } public String getValue() { return chars[index % chars.length] + Integer.toString(index / chars.length); } public String setValue(String value) { throw new UnsupportedOperationException(); } public int hashCode() { return Integer.valueOf(index).hashCode(); } } public Set<Map.Entry<Integer,String>> entrySet() { // LinkedHashSet retains initialization order: Set<Map.Entry<Integer,String>> entries = new LinkedHashSet<Map.Entry<Integer,String>>(); for(int i = 0; i < size; i++) entries.add(new Entry(i)); return entries; } public static void main(String[] args) { System.out.println(new CountingMapData(60)); } }
最终应该是在HashMap的 private abstract class HashIterator<E> implements Iterator<E>中实现的. 慢慢跟踪,挺麻烦的.
public boolean hasNext() {
return cursor != size();
}
public Object next() {
checkForComodification();
try {
Object next = get(cursor);
lastRet = cursor++;
return next;
} catch(IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
下面这段代码,使用的是 AbstractMap 的toString()方法,其中需要调用 entrySet() 方法中对象的iterator()。我想了解的是,其中,hasNext()、next()方法的具体实现是在哪里?public class CountingMapData
extends AbstractMap<Integer,String> {
private int size;
private static String[] chars =
"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"
.split(" ");
public CountingMapData(int size) {
if(size < 0) this.size = 0;
this.size = size;
}
private static class Entry
implements Map.Entry<Integer,String> {
int index;
Entry(int index) { this.index = index; }
public boolean equals(Object o) {
return Integer.valueOf(index).equals(o);
}
public Integer getKey() { return index; }
public String getValue() {
return
chars[index % chars.length] +
Integer.toString(index / chars.length);
}
public String setValue(String value) {
throw new UnsupportedOperationException();
}
public int hashCode() {
return Integer.valueOf(index).hashCode();
}
}
public Set<Map.Entry<Integer,String>> entrySet() {
// LinkedHashSet retains initialization order:
Set<Map.Entry<Integer,String>> entries =
new LinkedHashSet<Map.Entry<Integer,String>>();
for(int i = 0; i < size; i++)
entries.add(new Entry(i));
return entries;
}
public static void main(String[] args) {
System.out.println(new CountingMapData(60));
}
}
private abstract class HashIterator<E> implements Iterator<E>中实现的.
慢慢跟踪,挺麻烦的.