java - How to store object inside Iterator<object>? -
so have variable string , want store inside iterator of type string, keep getting error stating incompatible types. how can store string variable inside iterator?
incompatible types: required: java.util.iterator <java.lang.string> found: java.lang.string
this i've done far:
iterator<?> perentry = entries.iterator(); iterator<string> ids; while (perentry.hasnext()) { ids = perentry.next().getid(); }
any appreciated thank you!
p.s. forgot include this, how return iterator? error stating found java.util.objecttype, , requires java.util.iterator.
you misunderstood iterators. iterator not equivalent object in collection being iterated. instead, acts "pointer" such object.
if have iterator<entity>
, can entity
out of calling next()
. however, iterator remain iterator on entity
. cannot converted iterator of string
.
however, can harvest strings iterator collection, this:
list<string> idlist = new arraylist<>(); while (perentry.hasnext()) { idlist.add(perentry.next().getid()); }
once have idlist
list filled data, can iterator. iterator<string>
, because idlist
element of type string
:
iterator<string> ids = idlist.iterator();
Comments
Post a Comment