Collections / Generics Flashcards

1
Q

What are collections in Java?

A

The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.

Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.

Java Collection means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).

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

What are the interfaces in the Collections API?

A

The collection interfaces are divided into two groups. The most basic interface, java.util.Collection, has the following descendants:

java.util.Set
java.util.SortedSet
java.util.NavigableSet
java.util.Queue
java.util.concurrent.BlockingQueue
java.util.concurrent.TransferQueue
java.util.Deque
java.util.concurrent.BlockingDeque

The other collection interfaces are based on java.util.Map and are not true collections. However, these interfaces contain collection-view operations, which enable them to be manipulated as collections. Map has the following offspring:

java.util.SortedMap
java.util.NavigableMap
java.util.concurrent.ConcurrentMap
java.util.concurrent.ConcurrentNavigableMap

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

What is the difference between a Set and a List?

A

List is a type of ordered collection that maintains the elements in insertion order while Set is a type of unordered collection so elements are not maintained any order

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

What is the difference between an Array and an ArrayList?

A

The array is a specified-length data structure whereas ArrayList is a variable-length Collection class.

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

What is the difference between ArrayList and Vector?

A

Vector is synchronized, which means only one thread at a time can access the code, while ArrayList is not synchronized, which means multiple threads can work on ArrayList at the same time.

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

What is the difference between TreeSet and HashSet?

A

A TreeSet is a set where the elements are sorted. A HashSet is a set where the elements are not sorted or ordered

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

What is the difference between HashTable and HashMap?

A

HashMap allows one null key and multiple null values whereas Hashtable doesn’t allow any null key or value

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

Are Maps in the Collections API?

A

The Java platform contains three general-purpose Map implementations: HashMap, TreeMap, and LinkedHashMap

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

What makes Map different from other interfaces?

A

The Set interface provides an unordered collection of unique objects, i.e. Set doesn’t allow duplicates, while Map provides a data structure based on key-value pair and hashing.

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

List several ways to iterate over a Collection.

A

There are three common ways to iterate through a Collection in Java using either while(), for() or for-each()

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

How would you iterate over a Map?

A

five ways of iterating over a Map in Java

1.
Map.entrySet() method

2.
keySet() and values() methods

3.
Map.Entry<K, V> method

4.
forEach(action) method

5.
Iterating over keys and searching for values (inefficient)

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

What is the purpose of the Iterable interface?

A

allows an object to be the target of enhanced for loop(for-each loop)

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

What is the purpose of the Iterator interface?

A

allows us to access elements of the collection and is used to iterate over the elements in the collection(Map, List or Set)

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

What is the difference between the Comparable and Comparator interfaces?

A

While Comparable is used on objects that are naturally ordered, the Comparator interface implements sorting by taking the attributes of an object into consideration. Further, Comparator sorting takes into account objects of two different classes and Comparable compares objects using the “this” reference.

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

What are generics?

A

generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods

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

What is the diamond operator (<>)?

A

The diamond operator adds type inference and reduces the verbosity in the assignments – when using generics: List<String> cars = new ArrayList<>(); The inference feature determines the most suitable constructor declaration that matches the invocation.</String>

17
Q

Create and instantiate a generic class. Create and use a generic method

A

public class Box<T>
{ // T stands for "Type"
private T t;
public void set(T t) { this.t = t; }
public T get() { return t; }
}</T>