我想知道是否有办法检查迭代器对象。它是一种地图吗?
另外,有没有更好的方法来获取迭代器对象的内容并将它们放在mapC中?
这是我的代码:
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Map;
import java.util.Iterator;
public class Maps {
public static void main(String[] args) {
Map mapA = new HashMap();
Map mapB = new TreeMap();
mapA.put("key1", "element 1");
mapA.put("key2", "element 2");
mapA.put("key3", "element 3");
// The three put() calls maps a string value to a string key. You can then
// obtain the value using the key. To do that you use the get() method like this:
String element1 = (String) mapA.get("key1");
// why do I need the type cast on the right?
System.out.println(element1);
// Lets iterate through the keys of this map:
Iterator iterator = mapA.keySet().iterator();
System.out.println(iterator); // How to inspect this? Is it a kind of map?
Map mapC = new HashMap();
while(iterator.hasNext()){
Object key = iterator.next();
Object value = mapA.get(key);
mapC.put(key,value);
} // Is there a better way to take the contents of the iterator and put them in a new map?
System.out.println(mapC);
}
}
答案 0 :(得分:6)
使用迭代器唯一能做的就是调用hasNext()
,next()
和remove()
方法。每种不同类型的集合(和集合视图)的迭代器实现在每种情况下都可能不同;除此之外别无他法。
如其他地方所述,您可以使用mapC.putAll(mapA)
复制mapA
中的所有内容。但是,您通常应该使用泛型而不是原始类型。
答案 1 :(得分:3)
Iterator
是interface
,您无需检查。如果你想要有任何希望"检查"你必须知道实现接口的具体类。它。但请注意,Java API有多种实现(并且实现随着时间的推移而发展),因此通常无法预测将使用哪个具体类。
您根本不需要使用Iterator来创建HashMap的副本。可以使用复制构造函数HashMap(Map m)。
答案 2 :(得分:1)
使用Java反编译器,某些IDE有一个。例如,IntelliJ IDEA和Android Studio捆绑一个,它使您能够看到您无法看到的类。
或者您实际上可以检查公共Java API的源代码,因为Java源代码与JDK捆绑在一起,它位于src.zip
的安装文件夹中。
无论如何,这是HashMap
的迭代器实现。
private abstract class HashIterator {
int nextIndex;
HashMapEntry<K, V> nextEntry = entryForNullKey;
HashMapEntry<K, V> lastEntryReturned;
int expectedModCount = modCount;
HashIterator() {
if (nextEntry == null) {
HashMapEntry<K, V>[] tab = table;
HashMapEntry<K, V> next = null;
while (next == null && nextIndex < tab.length) {
next = tab[nextIndex++];
}
nextEntry = next;
}
}
public boolean hasNext() {
return nextEntry != null;
}
HashMapEntry<K, V> nextEntry() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
if (nextEntry == null)
throw new NoSuchElementException();
HashMapEntry<K, V> entryToReturn = nextEntry;
HashMapEntry<K, V>[] tab = table;
HashMapEntry<K, V> next = entryToReturn.next;
while (next == null && nextIndex < tab.length) {
next = tab[nextIndex++];
}
nextEntry = next;
return lastEntryReturned = entryToReturn;
}
public void remove() {
if (lastEntryReturned == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
HashMap.this.remove(lastEntryReturned.key);
lastEntryReturned = null;
expectedModCount = modCount;
}
}
private final class KeyIterator extends HashIterator
implements Iterator<K> {
public K next() { return nextEntry().key; }
}
private final class KeySet extends AbstractSet<K> {
public Iterator<K> iterator() {
return newKeyIterator();
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public boolean contains(Object o) {
return containsKey(o);
}
public boolean remove(Object o) {
int oldSize = size;
HashMap.this.remove(o);
return size != oldSize;
}
public void clear() {
HashMap.this.clear();
}
}
至于将一张地图的内容复制到另一张地图,there are questions about that kind of thing, but you can do it with a single putAll()
method call.