Java Collections Interview Questions and Answers


What is the Collection framework in Java?

Collection Framework is a grouping of classes and interfaces that is utilized to store and manage the objects. It provides various classes like  ArrayList, LinkedList, Vector, Treeset, PriorityQueue, HashSet, Stack, etc. Java Collection framework can withal be utilized for interfaces like Queue, Set, List, etc.

What is the difference between LinkedList and Arraylist?

ArrayList maintains indices like arrays. So if want more frequent get operations than put then ArrayList is best to go.
LinkedList maintains pointers to elements, we can't to a concrete index like in ArrayList. But the advantage here in LinkedList is that they don't require to shift back and forth like in ArrayList to maintain perpetual indices. So get operations in LinkedList are costly as you would have to go through pointers to reach your elements. But put operations are good as compared to ArrayList, we just need to connect to pointers and that's it.

What is the difference Between Hashset and Treeset?

1) First major distinction between HashSet and TreeSet is performance. HashSet is more expeditious than TreeSet and should be the preferred choice if sorting of elements is not required.
2) Second difference between HashSet and TreeSet is that HashSet sanctions null object but TreeSet doesn't sanction null Object and throw NullPointerException.
3) Another consequential distinction between HashSet and TreeSet is that HashSet is backed by HashMap while TreeSet is backed by NavigableMap in Java.

What is the difference between List and Set?

The List is an ordered collection (sequence), which typically allows duplicates
The Set  is a collection that contains no duplicate elements, iteration order may be guaranteed by the implementation

What is the difference between Set and Map?

1) Set contains values only whereas Map contains key and values both.
2) Set contains unique values whereas Map can contain unique Keys with duplicate values.
3) Set holds a single number of null value whereas Map can include a single null key with n number of null values.

What is the difference between Comparable and Comparator?

The comparator interface belongs to java. util package while comparable belongs to java.lang package. Comparator interface sort collection utilizing two objects provided to it, whereas comparable interface compares" this" refers to the one objects provided to it.

What is the hashCode()?

The hashCode() is a method that returns an integer hash code.

What is the benefit of Generics in the Collections Framework?

Generics allow us to provide the type of Object that a collection can contain, so if we try to add an element of other types it throws the compile-time error. This avoids ClassCastException at Runtime.

What are the basic interfaces of Java Collections Framework?

1) Collection
2) Set
3) List
4) Map

What is an Iterator?

An Iterator is an object that can be used to loop through collections, like ArrayList and HashSet. 

What is the difference between Iterator and ListIterator?

Iterator can traverse in forwarding direction only whereas ListIterator can be used to traverse in both directions.
ListIterator inherits from the Iterator interface and comes with extra functionalities like integrating an element, superseding an element, getting index position for previous and next elements.

What is the difference between fail-fast and fail-safe?

Iterator fail-safe property works with the clone of the underlying collection, hence it’s impervious to any modification in the collection. By design, all the collection classes in java. util package is fail-fast whereas collection classes in java.util.concurrent are fail-safe.
Fail-fast iterators throw ConcurrentModificationException whereas fail-safe iterator never throws ConcurrentModificationException.

What is the difference between Array and ArrayList? When will you use Array over ArrayList?

1) Arrays can contain primitive or Objects whereas ArrayList can contain only Objects.
2) Arrays are fixed-size whereas ArrayList size is dynamic.
3) Arrays don’t provide a lot of features like ArrayList, such as addAll, removeAll, iterator, etc.

How to iterate the Map?

1) Set<Map.Entry<K, V>>entrySet(): It is a method that returns a set having the entries mentioned in the map. These entries are generally objected, which has type Map. Entry.
2) Set<K>keySet(): This Java method returns a set that having the map key.

What is the main difference between Stack and Queue?

Stacks are predicated on the LIFO principle, i.e., the element inserted at the last, is the first element to emerge from the list.
Queues are predicated on the FIFO principle, i.e., the element inserted at the first, is the first element to emerge from the list.

Popular posts from this blog

Spring boot video streaming example-HTML5

DataTable-Pagination example with Spring boot, jQuery and ajax

10 Best Job Posting Sites 2021-2022

Spring boot web project free download:User Registration System

Spring Boot + JPA/Hibernate One to Many mapping example

5 Hardest Puzzle,100% fail answers

Java security AES,SHA256,SHA512,MD5-Spring Boot Project Free Download

Spring Boot-AngularJS-Bootstrap-JPA-CRUD

ReactJS - Bootstrap - Buttons

Spring Boot file upload/download example