Chapter 21: Collections Flashcards

1
Q

What is the collections API?

A

A group of classes and interfaces designed to store collections in various different ways. Allows elements to be added without worrying about memory allocation

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

What is the collection interface

A

Specifies the instance methods needed to support a collection: Size, Add, Remove, addAll, removeAll, retainAll, contains, containsAll, iterator

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

What constructor methods must a class implementing the collection interface provide?

A

One with no method argument builds empty collection.

One that takes existing collection and builds a new one containing the same elements

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

What is a list? What interface supports it?

A

A type of collection that allows duplicate elements, store in an ordering, each has a list index

List interface specifies instance methods to support list collection: size, add, get, set. Generic interface with type parameter of object to be stored

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

What is the iterator method specified in the list interface?

A

Returns an object that implements Iterator, supporting iteration of the elements in the list in ascending order of their index

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

What is an Array List?

A

Part of the collections framework. Implements the list interface. ArrayList implements List

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

What is a linked list?

A

Another implementation of the list interface using a doubly linked list. A generic class

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

What is a map? What is its interface?

A

Type of collection. Connects a key of any type to an element. Two keys can map to the same value.

Map interface specifies instance methods to support a map. Generic with types for keys and elements.
Put: takes key and element and adds to map. Old value replaced my new
Get: takes key and returns a value
Values: returns a collection of values. Iterator can support iterating through values depending on type
Keyset: returns set of keys in map

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

What is a Tree Map?

A

An implementation of map, uses ordered binary trees.

Values() gives collection, Iterator() of this gives iterator that supports iteration over values of map in key order.

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

What is a HashMap?

A

Part of collections framework. Implements map using a hash table. Each key must have appropriate implementation of haschode. Values() gives collection of values. Iteration in no particular order.

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

When do we use HashMap instead of TreeMap?

A

Don’t desire values in key order

Little or no hash code clashing. HashMap= constant time. TreeMap= logarithmic time.

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

Describe Sets and set interface

A

Kind of collection. Adding an already present element has no effect. Order added isn’t preserved.

Set interface specifies instance methods needed to support a set collection: size, add, contains

Iterator iterates over elements depending on the kind of set. May be an arbitrary order.

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

What is an iterator?

A

The instance method Iterator() returns an object that implements Iterator.

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

What is a TreeSet?

A

Part of collections framework. Implements set collection and interface. Uses an ordered binary tree so elements are ordered.

Iterator supports the iterator of elements in the order they appear in the tree, from left to right. We get the natural ordering of the elements.

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

When would we use HashSet instead of TreeSet?

A

When we don’t desire values in a specific order

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

What is a hashset?

A

A generic class that implements set interface. Uses a hash table with hash codes obtained from hashcode()

17
Q

What class methods are provided by collections?

A

Ones that perform manipulations of collections e.g. sort

18
Q

What is the iterator interface?

A

Specifies instance methods for accessing elements in a collection one by one:
hasNext
Next

19
Q

What is tree sort

A

Algorithm for sorting. Items from a list are inserted into ordered binary tree. Tree scanned from left to right. Can be achieved using an instance of TreeSet if data has no duplicates

20
Q

What is a hash table?

A

A method for storing data so that it can be retrieved quickly.
Places refrences to items in an array where the index is based on the has code provided by each item. Equivalent items have the same code.

Take hash code, Mod by size of array. Place item at that index.