Retrieving items from Collection
Retrieving items from Collection I have a Collection c, which contains 3 items. I want to assign these 3 items to 3 different variables Collection<Object> c; var1 = firstItem; var2 = secondItem; var3 = thirdItem; How can I extract the elements of the collection? There is no effort from OP. – pvpkiran Jun 29 at 9:05 2 Answers 2 You can do it as following: Collection<Object> collection = ...; Iterator iterator = collection.iterator(); Object firstItem = iterator.next(); Object secondItem = iterator.next(); Object thirdItem = iterator.next(); Update: This option is fine only if you are sure that collection contains at least 3 items. Otherwise, you need to check whether iterator has next item (method hasNext ) before calli...
