Collection Flashcards

1
Q

What is Java collection?

A

A collection is an object that contains multiple elements of the same type in a single unit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Java Collections Framework?

A

Collections framework is a library that provides common architecture for creating, updating and accessing different types of collections.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the main benefits of collections framework?

A

Reusability: common classes and utility methods that can be used with different types of collections.
Quality: improves the program quality, since the code is already tested.
Speed: developers speed increased since they can focus on core logic and use the collections provided.
Maintenance: Since collections are open source and widely documented, it is easy to maintain the code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the root of the collections framework?

A

Collection interface is the root of Collections Framework.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the thread safe classes in the Collections framework?

A

Stack, Vector, Hashtable, BlockingQueue, Concurrent Map, ConcurrentNavigableMap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to convert a list to a set?

A

Use HashSet. We can put a list into a HashSet, internally the hashCode() method is used to identify duplicate elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to remove duplicate elements from an ArrayList?

A

If ordering of elements is not important, then we can put the elements of ArrayList into a HashSet and then add them back to the ArrayList.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you make the elements of a Collection be in sorted order?

A

You can use the utility method Collections.sort to sort a list. This is nLog(n) order of performance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In which scenario is LinkedList better than ArrayList?

A

LinkedList is better in the scenario when we do not need random access to elements, or if there are a lot of insertion or deletion actions on elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the differences between list & set?

A

Order: list is an ordered sequence of elements. Set is just a distinct collection that is unordered.
Positional Access: When using a list we can specify where we want to insert an element. In a set there is no order.
Duplicate: in a list we can store duplicates. In a set we cannot store duplicates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to decide when to use List, Set or Map?

A

If we do not want duplicates, use a set. If we want to frequently access elements operations based on an index value or we want to maintain insertion order then we used a list. For fast key-based search we use a HashMap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Difference between HashMap & HashTable?

A

Synchronisation: HashMap is not synchronised. HashTable is synchronised.
Null values: HashMap only allows one null key and n null values. HashTable does not allow null keys or values.
Ordering: HashMap maintains the order of elements. A HashTable does not guarantee any kind of order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the reason for overriding the equals method?

A

The equals method in Object class is used to check wether two objects are the same or not. If we want a custom implementation we override it. In HashMap implementation, if we want to use an object as a key, we override it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does hashCode() method work in java?

A

Object class in Java has hashCode() method which returns a hash code value, which is an integer. If two objects are the same, their hashcode is also the same. Object generates a hashcode based on the memory address of the instance of the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between Iterator and Enumeration?

A

Both are interfaces in java to access Data Structures. Enumeration is an older interface. Enumeration can only traverse legacy collections. Iterator can traverse both legacy and newer collections.Iterator is fail-fast, enumeration is not fail-fast.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between an ArrayList and LinkedList

A

ArrayList is an index based dynamic array. LinkedList is a Doubly Linked List data structure. It is easier to insert into LinkedList, a since there is no need to resize an Array. LinkedList has better performance for removing elements. LinkedList is slower in accessing an element, since we have to traverse the list one by one to access the right location.

17
Q

What is the default size of the load factor in a HashMap?

A

0.75

18
Q

What is a Hash collision?

A

A hash collision occurs when two objects may have the same HashCode but they are different. Java will have an issue storing the two different objects in a HashMap in this case.

19
Q

Difference between queue and stack?

A

Queue is FIFO. Stack is LIFO.

20
Q

What method do we need to override to use an object as a key in a hashmap?

A

equals() and hashCode()

21
Q

What is the difference between peek(), poll() and remove() methods of Queue?

A

poll() and remove() can be used for removing the head object of a queue. If the list is empty the poll() method returns null, but the remove() throws a NoSuchElementException. peek() is used to retrieve the head of the queue, but not remove it.

22
Q

How to insert, delete & retrieve elements from a HashMap?

A

Retrieve: get() method, passing object key.
Insert: put() method, passing key & value
Delete: remove() method, passing object key.

23
Q

What is the difference between fail-fast and fail-safe Iterator in java?

A

Fail-fast iterator throws ConcurrentModificationException, fail safe does not throw this exception. Fail-fast does not clone the original collection, fail safe creates a copy of the original collection of objects.

24
Q

How to convert a map to a list in java?

A

A map has three collection sets, key set, value set, key-value set. We can use List keyList = new ArrayList(map.keySet());