0% found this document useful (0 votes)
8 views2 pages

Java_Collection_Framework_Questions

Uploaded by

prernajangra1124
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
8 views2 pages

Java_Collection_Framework_Questions

Uploaded by

prernajangra1124
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2

What are the two ways to iterate the elements of a collection?

The two main ways to iterate the elements of a collection in Java are:
1. Using Iterator: The Iterator interface provides methods to iterate over any Collection. It
has methods like hasNext(), next(), and remove().
2. Using enhanced for loop (for-each loop): This is a more readable and compact way to
iterate over elements of arrays and collections.

What is the difference between ArrayList and LinkedList classes in collection


framework?
ArrayList and LinkedList are both implementations of the List interface but have different
underlying data structures:
1. ArrayList: Uses a dynamic array to store the elements. It provides fast random access but
slow insertions and deletions (except at the end of the list).
2. LinkedList: Uses a doubly-linked list to store the elements. It provides faster insertions
and deletions but slower random access.

What is the difference between ArrayList and Vector classes in collection


framework?
ArrayList and Vector are both implementations of the List interface but have some key
differences:
1. Synchronization: Vector is synchronized, making it thread-safe, while ArrayList is not
synchronized.
2. Performance: Due to synchronization, Vector is slower than ArrayList.
3. Growth: Vector increases its size by doubling the array size when it gets full, whereas
ArrayList increases its size by 50%.

What is the difference between HashSet and HashMap classes in collection


framework?
HashSet and HashMap are both used for storing collections of objects but have different
purposes and structures:
1. HashSet: Implements the Set interface and stores unique elements. It uses a HashMap
internally to store its elements.
2. HashMap: Implements the Map interface and stores key-value pairs. Keys must be unique.

What is the difference between HashMap and Hashtable class?


HashMap and Hashtable both implement the Map interface but have several differences:
1. Synchronization: Hashtable is synchronized and thread-safe, while HashMap is not.
2. Null values: HashMap allows one null key and multiple null values, while Hashtable does
not allow any null key or value.
3. Legacy: Hashtable is considered a legacy class and part of the original version of Java,
whereas HashMap was introduced in Java 1.2.
What is the difference between Iterator and Enumeration interface in collection
framework?
Iterator and Enumeration are both used to iterate over elements in a collection, but they
have differences:
1. Methods: Enumeration has two methods (hasMoreElements() and nextElement()), while
Iterator has three methods (hasNext(), next(), and remove()).
2. Functionality: Iterator allows the caller to remove elements from the underlying
collection, which Enumeration does not.
3. Legacy: Enumeration is considered a legacy interface, while Iterator is preferred in newer
code.

How can we sort the elements of an object? What is the difference between
Comparable and Comparator interfaces?
To sort the elements of an object, you can use:
1. Comparable: The object itself implements the Comparable interface and overrides the
compareTo() method. This allows objects to be compared based on one natural ordering.
2. Comparator: A separate class implements the Comparator interface and overrides the
compare() method. This allows objects to be compared based on multiple orderings.

What does the hashcode() method?


The hashcode() method returns an integer value, generated by a hashing algorithm. This
value is used by hash-based collections like HashMap, HashSet, and Hashtable to store and
retrieve objects efficiently. The hashcode() method must obey the general contract that
equal objects must have the same hash code.

What is the difference between Java collection and Java collections?


Java collection refers to the single unit of objects, i.e., it is a framework that provides
architecture to store and manipulate the group of objects. Java Collections is a utility class in
java.util package that provides static methods to operate on collections, such as sorting and
searching.

You might also like