Search Interview Questions | ![]() ![]() Click here and help us by providing the answer. ![]() Click Correct / Improve and please let us know. |
|
| ||||
Core Java - Interview Questions and Answers for 'Iterator' - 11 question(s) found - Order By Newest | ||||
![]() | ||||
| ||||
Ans. Iterator is an interface that provides methods to iterate over any Collection. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. http://www.buggybread.com/2015/01/java-iterator-classes-and-interfaces.html | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Fail-fast iterators detect illegal concurrent modification during iteration and fail quickly and cleanly rather than risking arbitrary, non deterministic behavior at an undetermined time in future. Example could be of an Iterator failing if it smells ConcurrentModificationException. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. No, iterator is an interface that is used to parse through the elements of a Collection | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Though there are many differences the way internally they both iterates the collections and streams respectively, but the main difference in performance that is achieved by spliterator as it can iterate Streams in Parallel whereas iterator only iterates collections sequentially. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Spliterator has better performance potential than iterators but only if the potential is used. Spliterator can iterate streams in parallel as well as in sequence whereas iterator can only iterate in sequence. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Enumeration Iterator List iterator | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Enumeration can iterate only legacy collections like Vector , HashTable and Stack whereas Iterator can iterate both legacy and non legacy collections. Enumeration is less safer than Iterator Enumeration is fail safe whereas Iterator is fail fast Iterator allows for removal of element while traversal whereas Enumeration doesn't have remove method. Enumerations were introduced in Java 1 whereas Iterators were introduced with Java 2 Enumerations have methods like hasMoreElements and nextElement whereas Iterators have methods like hasNext, next and remove | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. This can be done using a Spliterator. LinkedList list = Arrays.asList("names","numbs","birds","animals"); Spliterator split1 = list.Spliterator(); Spliterator split2 = split1.Spliterator(); Now, the LinkedList is split into split1 and split2. use split2 first then split1 to check the output. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. No,we cannot.I t will give concurrentModificationExceptin error. It can be resolved by using ConcurrentClasses like ConcurrentHashMap,CopyOnWriteArrayList,BlockingQueue etc which are fail-safe and wont give exception. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. CustomArrayList myarrayList = new CustomArrayList(); myarrayList.add("Value 1"); myarrayList.add("Value 2"); myarrayList.add("Value 3"); for (String string : myarrayList) { System.out.println(string); } package sample.utils; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class CustomArrayList implements Iterable { private ArrayList mylist = new ArrayList(); public void add(T t) { this.mylist.add(t); } @Override public Iterator iterator() { return new CustomIterator(mylist); } class CustomIterator implements Iterator { private int indexPosition = 0; private List internalList; public CustomIterator(List internalList) { this.internalList = internalList; } @Override public boolean hasNext() { if (internalList.size() >= indexPosition 1) { return true; } return false; } @Override public E next() { E val = internalList.get(indexPosition); indexPosition ; return val; } } } | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||