Collections/Genetics Flashcards

1
Q

What are collections in Java?

A

They are a framework that provides an architecture to store and manipulate a group of objects.

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

Set, List, Queue, Deque

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

A set is unordered and cant have duplicates, while a List is ordered and can contain duplicates

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

An array is an object that has elements of similar data types with a set size. An ArrayList provides a dynamically sized array

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

They both use Arrays internally as a data structure and resize dynamically, but a Vector doubles in size when it is resized whereas an ArrayList increases by half of its size

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

HashSet does not maintain any order in the set and are inserted based on their hashcode, TreeSet is sorted by ascending order by default

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 is non-synchronized, not thread safe, and cannot be shared between threads without proper synchronization, whereas HashTable is synchronized and thread safe. HashMap allows one null key and multiple null values, whereas HashTable doesnt allow any null keys or values

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

Are Maps in the Collections API? What makes Map different from other interfaces?

A

Map is not part of the Collections API. Maps store key value pairs while other interfaces store single values

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

List several ways to iterate over a Collection. How would you iterate over a map?

A

We can use while loops, for loops, enhanced for loops, the forEach() method, or using an iterator

We cannot iterate a map directly with an iterator. You can iterate over Map.entrySet() using a for-each loop, Using keySet() and values() methods, using iterators over Map.Entry, Iterating over keys and searching for values

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

What is the purpose of the Iterable interface? What about Iterator?

A

The iterable interface is the root interface for all collection classes. It provides the ability to iterate the elements in a forward direction only
An iterator is an object that can be used to loop through collections like ArrayList and HashSet

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

What is the difference between the Comparable and Comparator interfaces?

A

The Comparator interface sorts collections using two objects provided to it, whereas the Comparable interface compares “this” which refers to one of the objects provided to it

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

What are generics? What is the diamond operator <>?

A

Generics means parameterized types, which allows a type to be a parameter for a method, class, or interface. The diamond operator simplifies the use of generics when creating an object

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