0% found this document useful (0 votes)
10 views

Collections

Uploaded by

Harsh mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Collections

Uploaded by

Harsh mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Tweet

Your Account | Advertise with us | Feedback |


Few pages of this site are under construction and we are constantly trying to improve it. If you have any suggestions or ideas about how to improve the site, please Let us know.

Home
Core Java Interview Questions-2 Test
Home

Articles OOPS Interview Questions Java Basic Interview Questions


Articles Java Exceptions Interview Questions Java Collections Interview Questions
Tutorials
Java Threads Interview Questions Java Generics Interview Questions
Spring Tutorials
Java Annotations Interview Questions Java I/O Interview Questions
iBatis Tutorials

JSF-MyFaces Tag Ref. « Previous | 1 2 3 4 5 | Next »


JSF Tutorials

Hibernate Tutorials Java Collections Interview Questions


Struts Tutorials
46. What is an Iterator ?
More....
The Iterator interface is used to step through the elements of a
Interview Questions Collection.
Iterators let you process each element of a Collection.
Spring Questions
Iterators are a generic way to go through all the elements of
JSF Questions a Collection no matter how it is organized.
Hibernate Questions
Iterator is an Interface implemented a different way for
every Collection.
Struts Questions

AJAX Questions

JSP Questions 47. How do you traverse through a collection using its Iterator?
To use an iterator to traverse through the contents of a collection,
Servlets Questions
follow these steps:
JDBC Questions Obtain an iterator to the start of the collection by calling the
Core Java Questions collection’s iterator() method.
Set up a loop that makes a call to hasNext(). Have the loop iterate
XML Questions
as long as hasNext() returns true.
Webservices Questions Within the loop, obtain each element by calling next().
EJB Questions

webMethods Questions
48. How do you remove elements during Iteration?
Tibco Questions Iterator also has a method remove() when remove is called, the current element in the iteration is deleted.
BREW Questions

More.... 49. What is the difference between Enumeration and Iterator?


Certifications Enumeration Iterator
Spring Certification
Enumeration doesn't have a remove() method Iterator has a remove() method
SCJP 6.0
Enumeration acts as Read-only interface, because it has the Can be abstract, final, native, static, or synchronized
SCJP 5.0 methods only to traverse and fetch the objects
SCJP 1.4 Note: So Enumeration is used whenever we want to make Collection objects as Read-only.
SCWCD

SCBCD 50. How is ListIterator?


SCEA ListIterator is just like Iterator, except it allows us to access the
collection in either the forward or backward direction and lets us
SCMAD
modify an element
SCDJWS

J2EE 51. What is the List interface?


WebSphere The List interface provides support for ordered collections of
XML
objects.
Lists may contain duplicate elements.
PMP

UML

webMethods 52. What are the main implementations of the List interface ?
The main implementations of the List interface are as follows :
Struts
ArrayList : Resizable-array implementation of the List interface.
More....
The best all-around implementation of the List interface.
Vector : Synchronized resizable-array implementation of the List
interface with additional "legacy methods."
LinkedList : Doubly-linked list implementation of the List interface. May provide better performance than the ArrayList
implementation if elements are frequently inserted or deleted within the list. Useful for queues and double-ended queues
(deques).

53. What are the advantages of ArrayList over arrays ?


Some of the advantages ArrayList has over arrays are:
It can grow dynamically
It provides more powerful insertion and search mechanisms than arrays.

54. Difference between ArrayList and Vector ?


ArrayList Vector
ArrayList is NOT synchronized by default. Vector List is synchronized by default.
Vector list can use Iterator and Enumeration Interface to
ArrayList can use only Iterator to access the elements.
access the elements.
The ArrayList increases its array size by 50 percent if it runs A Vector defaults to doubling the size of its array if it runs
out of room. out of room
ArrayList has no default size. While vector has a default size of 10.

55. How to obtain Array from an ArrayList ?

Array can be obtained from an ArrayList using toArray() method on ArrayList.


List arrayList = new ArrayList();
arrayList.add(…

Object a[] = arrayList.toArray();

56. Why insertion and deletion in ArrayList is slow compared to LinkedList ?


ArrayList internally uses and array to store the elements, when that array gets filled by inserting elements a new array of
roughly 1.5 times the size of the original array is created and all the data of old array is copied to new array.
During deletion, all elements present in the array after the deleted elements have to be moved one step back to fill the space
created by deletion. In linked list data is stored in nodes that have reference to the previous node and the next node so
adding element is simple as creating the node an updating the next pointer on the last node and the previous pointer on the
new node. Deletion in linked list is fast because it involves only updating the next pointer in the node before the deleted node
and updating the previous pointer in the node after the deleted node.

57. Why are Iterators returned by ArrayList called Fail Fast ?


Because, if list is structurally modified at any time after the iterator is
created, in any way except through the iterator's own remove or add
methods, the iterator will throw a ConcurrentModificationException.
Thus, in the face of concurrent modification, the iterator fails quickly
and cleanly, rather than risking arbitrary, non-deterministic behavior at
an undetermined time in the future.

58. How do you decide when to use ArrayList and When to use
LinkedList?
If you need to support random access, without inserting or removing
elements from any place other than the end, then ArrayList offers the
optimal collection. If, however, you need to frequently add and remove
elements from the middle of the list and only access the list elements
sequentially, then LinkedList offers the better implementation.

« Previous | 1 2 3 4 5 | Next »

ALSO SEE :

About Us | Our Mission | Terms of use | Author Terms | Search | Contact Us | Advertise With Us | Link to Us | Sitemap
Copyright © 2016 developersBOOK.COM. All rights reserved.

You might also like