Author Archives: Stanislav_Panteleev

Java.Collections.How will an instantiated iterator for collection behave if collection.remove() is called?

👉 Short Answer: If the collection is structurally modified outside the iterator (like calling collection.remove() directly), the iterator becomes invalid, and a ConcurrentModificationException will be thrown on the next iterator operation.

Posted in Без рубрики | Comments Off on Java.Collections.How will an instantiated iterator for collection behave if collection.remove() is called?

Java.Core.How will collection behave if iterator.remove() is called?

👉 Short Answer: It removes the last element returned by next() from the underlying collection.

Posted in Без рубрики | Comments Off on Java.Core.How will collection behave if iterator.remove() is called?

Java.Collections.How many elements will be skipped if Iterator.next() is called after 10 calls to Iterator.hasNext()?

👉 Answer: None. Zero elements are skipped. 🔍 Why? Because: hasNext() is a peek method — it does not move the iterator forward. Only next() advances the iterator to the next element. So no matter how many times you call … Continue reading →

Posted in Без рубрики | Comments Off on Java.Collections.How many elements will be skipped if Iterator.next() is called after 10 calls to Iterator.hasNext()?

Java.Core.What happens if you call Iterator.next() without first calling Iterator.hasNext()?

❓ What happens if you call iterator.next() without hasNext()? 👉 Answer: If there is a next element, then next() simply returns it — no problem. But if the iterator is already at the end, and you don’t check hasNext(), then … Continue reading →

Posted in Без рубрики | Comments Off on Java.Core.What happens if you call Iterator.next() without first calling Iterator.hasNext()?

Java.Collections.What about fail fast in list iterator

💥 Is ListIterator fail-fast? ✅ Yes, ListIterator is fail-fast, just like Iterator. It monitors structural changes to the list during iteration using an internal modCount, and if the list is modified outside of the iterator while iterating, it throws:

Posted in Без рубрики | Comments Off on Java.Collections.What about fail fast in list iterator

Java.Collections.Compare Iterator and ListIterator.

Feature Iterator ListIterator Applicable to All Collection types Only List types Direction of iteration 🔁 Forward only 🔁 Forward and backward Can remove elements? ✅ Yes (remove()) ✅ Yes (remove()) Can add elements? ❌ No ✅ Yes (add()) Can replace … Continue reading →

Posted in Без рубрики | Comments Off on Java.Collections.Compare Iterator and ListIterator.

Java.How are Iterable, Iterator, and “for-each” related?

Ahh, perfect! Now you’re tying all the pieces together, Stanley — this is exactly how you build a solid mental model. Let’s connect Iterable, Iterator, and the for-each loop in Java.

Posted in Без рубрики | Comments Off on Java.How are Iterable, Iterator, and “for-each” related?

Java.Collections.How are Iterable and Iterator related?

Awesome question again, Stanley! This one gets to the heart of how iteration works in Java’s Collection Framework. Let’s break down how Iterable and Iterator are related, what their roles are, and how they work together.

Posted in Без рубрики | Comments Off on Java.Collections.How are Iterable and Iterator related?

Java.Collections.What is the difference between Enumeration and Iterator.

Great follow-up, Stanley! Enumeration and Iterator are both used to traverse elements in a collection, but they come from different generations of the Java Collection Framework, and they have key differences in capability, safety, and flexibility.

Posted in Без рубрики | Comments Off on Java.Collections.What is the difference between Enumeration and Iterator.

Java.Collections.

🛡️ Fail-Safe Iterators — Real Examples 1. ✅ CopyOnWriteArrayList.iterator() Part of the java.util.concurrent package. Iterator works on a snapshot copy of the array. Safe to modify the list while iterating — changes do not affect the iterator.

Posted in Без рубрики | Comments Off on Java.Collections.