Java Collections Interview Questions

Source: Internet
Author: User
Tags comparable java collections interview questions

Java Collections Interview Questions

In Java, collection interview questions is mostly asked by the interviewers. Here are the list of mostly asked collections interview questions with answers.

1) What is the difference between ArrayList and Vector?
No. ArrayList Vector
1) ArrayList is not synchronized. Vector is synchronized.
2) ArrayList is a legacy class. Vector is a legacy class.
3) ArrayList increases its size by 50% of the array size. Vector increases its size by doubling the array size.
2) What is the difference between ArrayList and LinkedList?
No. ArrayList LinkedList
1) ArrayList uses a dynamic array. LinkedList uses doubly linked list.
2) ArrayList is not efficient for manipulation because a lot of shifting are required. LinkedList is efficient for manipulation.
3) ArrayList is better to store and fetch data. LinkedList is better to manipulate data.
3) What is the difference between Iterator and listiterator?

Iterator traverses the elements in forward direction only whereas Listiterator traverses the elements in forward and backw ARD direction.

No. Iterator Listiterator
1) Iterator traverses the elements in forward direction only. Listiterator traverses the elements in backward and forward directions both.
2) Iterator can is used in List, Set and Queue. Listiterator can used in List only.
4) What is the difference between Iterator and enumeration?
No. Iterator Enumeration
1) Iterator can traverse legacy and non-legacy elements. Enumeration can traverse only legacy elements.
2) Iterator is fail-fast. Enumeration is not fail-fast.
3) Iterator is slower than enumeration. Enumeration is faster than Iterator.
5) What is the difference between List and Set?

List can contain duplicate elements whereas Set contains only unique elements.

6) What is the difference between HashSet and TreeSet?

HashSet maintains no order whereas TreeSet maintains ascending order.

7) What is the difference between Set and Map?

Set contains values only whereas MAP contains key and values both.

8) What is the difference between HashSet and HashMap?

HashSet contains only values whereas HashMap contains entry (key,value). HashSet can iterated but HashMap need to convert into Set to be iterated.

9) What is the difference between HashMap and TreeMap?

HASHMAP maintains no order but TreeMap maintains ascending order.

Ten) What is the difference between HashMap and Hashtable?
No. HashMap Hashtable
1) HashMap is not synchronized. Hashtable is synchronized.
2) HashMap can contain one null key and multiple null values. Hashtable cannot contain any null key or null value.
One) What is the difference between Collection and collections?

Collection is a interface whereas collections is a class. Collection interface provides normal functionality of data structure to List, Set and Queue. But, collections class was to sort and synchronize collection elements.

What is the difference between comparable and Comparator?
No. comparable Comparator
1) Comparable provides only one sort of sequence. Comparator provides multiple sort of sequences.
2) It provides one method named CompareTo (). It provides one method named Compare ().
3) It is the found in Java.lang package. It is the found in Java.util package.
4) If we implement comparable interface, actual class is modified. Actual class is not modified.
What is the advantage of Properties file?

If you change the value in properties file, you don ' t need to recompile the Java class. So, it makes the application easy to manage.

What does the Hashcode () method?

The Hashcode () method returns a hash code value (an integer number).

The Hashcode () method returns the same integer number, if the keys (by calling Equals () method) is same.

But, it's possible that the hash code numbers can has different or same keys.

Why we override Equals () method?

The Equals method is used to check whether-objects is same or not. It needs to is overridden if we want to check the objects based on property.

For example, the Employee is a class which has 3 data members:id, name and salary. But, we want to check the equality of an employee object on the basis of salary. Then, we need to override the Equals () method.

How to synchronize List, Set and Map elements?

Yes, Collections class provides methods to make List, Set or Map elements as synchronized:

public static list synchronizedlist (list L) {}
public static set Synchronizedset (set s) {}
public static SortedSet Synchronizedsortedset (SortedSet s) {}
public static map Synchronizedmap (map m) {}
public static SortedMap Synchronizedsortedmap (SortedMap m) {}
What is the advantage of generic collection?

If we use generic class, we don ' t need typecasting. It is typesafe and checked at compile time.

What's hash-collision in Hashtable and how it's handled in Java?

The different keys with the same hash value are known as hash-collision. Both different entries'll be kept in a single hash buckets to avoid the collision.

What is the Dictionary class?

The Dictionary class provides the capability to store key-value pairs.

What's the default size of load factor in hashing based collection?

The default size of load factor is 0.75. The default capacity is computed as initial capacity * load factor. For example, 16 * 0.75 = 12. So, the default capacity of Map.

What is the difference between Array and ArrayList? What is the difference between length of Array and size of ArrayList? How to convert ArrayList to array and array to ArrayList? How to make Java ArrayList read-only? How to remove duplicates from ArrayList? How to reverse ArrayList? How to sort ArrayList in descending order? How to synchronize ArrayList? When to use ArrayList and LinkedList?Next TopicJDBC interview Questions

Java Collections Interview Questions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.