Chapter 21: Collections Flashcards
What is the collections API?
A group of classes and interfaces designed to store collections in various different ways. Allows elements to be added without worrying about memory allocation
What is the collection interface
Specifies the instance methods needed to support a collection: Size, Add, Remove, addAll, removeAll, retainAll, contains, containsAll, iterator
What constructor methods must a class implementing the collection interface provide?
One with no method argument builds empty collection.
One that takes existing collection and builds a new one containing the same elements
What is a list? What interface supports it?
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
What is the iterator method specified in the list interface?
Returns an object that implements Iterator, supporting iteration of the elements in the list in ascending order of their index
What is an Array List?
Part of the collections framework. Implements the list interface. ArrayList implements List
What is a linked list?
Another implementation of the list interface using a doubly linked list. A generic class
What is a map? What is its interface?
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
What is a Tree Map?
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.
What is a HashMap?
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.
When do we use HashMap instead of TreeMap?
Don’t desire values in key order
Little or no hash code clashing. HashMap= constant time. TreeMap= logarithmic time.
Describe Sets and set interface
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.
What is an iterator?
The instance method Iterator() returns an object that implements Iterator.
What is a TreeSet?
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.
When would we use HashSet instead of TreeSet?
When we don’t desire values in a specific order