Java Iterators. How to inspect the Iterator? -
i want know if there way inspect iterator object. kind of map?
also, there a better way take contents of iterator object , put them in mapc?
here code:
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"); // 3 put() calls maps string value string key. can // obtain value using key. use get() method this: string element1 = (string) mapa.get("key1"); // why need type cast on right? system.out.println(element1); // lets iterate through keys of map: iterator iterator = mapa.keyset().iterator(); system.out.println(iterator); // how inspect this? kind of map? map mapc = new hashmap(); while(iterator.hasnext()){ object key = iterator.next(); object value = mapa.get(key); mapc.put(key,value); } // there better way take contents of iterator , put them in new map? system.out.println(mapc); } }
the thing can iterator call hasnext(), next(), , remove() methods. iterator implementations every different type of collection (and collection view) different in every case; there's nothing else can them.
as stated elsewhere, can use mapc.putall(mapa) copy on contents mapa. should use generics instead of raw types, though.
Comments
Post a Comment