20 Java Collections Interview Questions

Posted VickyFengYu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了20 Java Collections Interview Questions相关的知识,希望对你有一定的参考价值。

20 Java Collections Interview Questions

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


1) What is the difference between ArrayList and Vector?

No.ArrayListVector
1) ArrayList is not synchronized. Vector is synchronized.
2) ArrayList is not 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.ArrayListLinkedList
1) ArrayList uses a dynamic array. LinkedList uses doubly linked list.
2) ArrayList is not efficient for manipulation because a lot of shifting is 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 backward direction.

No.IteratorListIterator
1) Iterator traverses the elements in forward direction only. ListIterator traverses the elements in backward and forward directions both.
2) Iterator can be used in List, Set and Queue. ListIterator can be used in List only.

4) What is the difference between Iterator and Enumeration?

No.IteratorEnumeration
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 be 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.


10) What is the difference between HashMap and Hashtable?

No.HashMapHashtable
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.

11) What is the difference between Collection and Collections?

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


12) What is the difference between Comparable and Comparator?

No.ComparableComparator
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 found in java.lang package. it is found in java.util package.
4) If we implement Comparable interface, actual class is modified. Actual class is not modified.

13) 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.


14) 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 two keys (by calling equals() method) are same.

But, it is possible that two hash code numbers can have different or same keys.


15) Why we override equals() method?

The equals method is used to check whether two objects are same or not. It needs to be overridden if we want to check the objects based on property.

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


16) 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){}

17) 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.


18) What is hash-collision in Hashtable and how it is handled in Java?

Two different keys with the same hash value is known as hash-collision. Two different entries will be kept in a single hash bucket to avoid the collision.


19) What is the Dictionary class?

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


20) What is 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, 12 is the default capacity of Map.


21) What is the difference between Array and ArrayList?


22) What is the difference between length of Array and size of ArrayList?


23) How to convert ArrayList to Array and Array to ArrayList?


24) How to make Java ArrayList Read-Only?


25) How to remove duplicates from ArrayList?


26) How to reverse ArrayList?


27) How to sort ArrayList in descending order?


28) How to synchronize ArrayList?


29) When to use ArrayList and LinkedList?

以上是关于20 Java Collections Interview Questions的主要内容,如果未能解决你的问题,请参考以下文章

java基础(20):Map可变参数Collections

Java20统计字符出现个数(断点debug),斗地主,自定义Collections类和TreeSet

Java 中Collections工具类的使用

Java入门与基础(collections接口实例(结合setlist))

在集群的个人账户下安装20版本 lammps(2019版本inter编译器,lammps安装版本为3Mar2020, 集群版本为Centos 7.5)

Java相关面试题总结+答案