Java Collections Framework Flashcards

1
Q

Java collections framework

A

A hierarchy of interface types and classes for collecting objects. At the root of this hierarchy is the Collection interface. Because all collections implement this interface, its methods are available for all collection classes. For example, the size method reports the number of elements in any collection.

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

List

A

A list is a collection that remembers the order of its elements. In Java, the ArrayList class, the Stack class, and the LinkedList class implement the List interface.

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

Set

A

A set is an unordered collection of unique elements. Because a set does not track the order of its elements, it can arrange them so that the operations of finding, adding, and removing elements is most efficient. In Java, the HashSet and TreeSet classes implement the Set interface.

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

Stack

A

A stack remembers the order of its elements, but it does not allow you to insert elements in every position. You can only add and remove elements at the top.

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

Queue

A

In a queue, you add items to one end (the tail) and remove them from the other end (the head).

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

Priority queue

A

A priority queue is an unordered collection that has an efficient operation for removing the element with the highest priority. In Java, the PriorityQueue class implements the Queue interface.

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

Map

A

A map manages associations between keys and values. Every key in the map has an associated value. The map stores the key, the values, and the associations between them. In Java, the HashMap and TreeMap classes implement the Map interface.

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

LinkedList

A

A data structure that is used for collecting a sequence of objects that allows efficient additional and removal of elements in the middle of the sequence. A LinkedList consists of a number of nodes, each of which is an object that stores an element and has a reference to the next node. Therefore, when you insert a new node into a LinkedList, only the neighboring node references need to be updated.

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

What is one advantage of a LinkedList compared to an ArrayList?

A

Unlike an ArrayList, a LinkedList allows efficient insertion and removal of elements in the middle of the list. (Remember that when an element is added or removed from an an ArrayList, the elements at larger positions must be moved.)

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

What is one drawback of a LinkedList?

A

Element access can be inefficient because if you want to access the fifth element in a LinkedList, then you must first traverse the first four. Therefore you use a LinkedList when you are concerned about the efficiency of adding or removing elements as opposed to accessing them in a random order.

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

What are the most commonly used methods of the LinkedList class?

A

There are in built methods for accessing, adding or removing elements at the beginning or end of the LinkedList:

  • addLast
  • addFirst
  • getFirst
  • getLast
  • removeFirst
  • removeLast

* Note, to access other elements you need to use an iterator.

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

What is the syntax for declaring a new HashMap?

A

HashMap<Integer, String> names = new HashMap<*I**nteger, String>( );

*Where Integer is the data type of key, String is the data type of the value, and names is the reference variable for the HashMap.

** Note that the key HAS TO BE UNIQUE!*

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

Iterator

A

An iterator encapsulates a position anywhere inside the LinkedList. Initially it points before the first element. However, you can move the iterator position with the next() method:
iterator.next();

* Note that the next() method throws a NoSuchElementException if you are already past the end of the list so you should always call the iterator’s hasNext() method before calling the next() method.

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

What is an essential difference between a collection and a set?

A

A set does not admit duplicates! If you try to add an element to a set that’s already present, the insertion is ignored. Similarly, attempting to remove an element that isn’t present in the set is ignored. You can use the contains method to test if an element is contained in the set. For example:
if (names.contains(“Juliet”))

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

Common methods for working with sets

A
  • .add()
  • .remove()
  • .contains()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Common methods for working with maps

A
  • .put() - adds keys and values to the map
  • .get() - returns the value associated with the key or null if the key is not present
  • .remove() - removes the key and value
17
Q

How do you choose the right collection to use?

A
  1. Determine how to access the values. You have a few options:
    • If values are accessed by an integer position, use an ArrayList
    • If values are accessed by a key that is not part of the object, use a map
    • If values are accessed only at one of the ends, use a queue for FIFO or a stack for LIFO
    • If you don’t need to access individual values by position, refine you choice in steps 3 and 4
  2. Determine the element types or key / value types that you want to store.
  3. Determine whether element or key order matters. You have several choices:
    • If elements or keys must be sorted, use a TreeSet or TreeMap
    • If elements must be in the same order in which they were inserted, then use an ArrayList or LinkedList
    • If order doesn’t matter and you chose a map in step 1, then proceed to step 5
  4. For a collection, determine which operations must be efficient
    • If finding elements must be efficient, use a HashSet
    • If it must be efficient to add or remove elements, then use a LinkedIn
    • If you only insert or remove at the end or you’re not concerned about speed, then use an ArrayList
  5. For hash sets and maps, decide whether you need to implement the hashCode and equals methods. The classes in the standard Java library will mostly all have these already, but if you’re not using one of these then you’ll need to check to confirm.
  6. If you use a tree, decide whether to supply a comparator. Most classes in the standard Java library already implement the Comparable interface and have a compareTo method, but if you’re not using one of these then you’ll need to check to confirm.
18
Q

Hash function / hash code

A
A function that computes a hash code, a unique integer value, for an object. A good hash function minimizes collisions so no two objects have the same hash code. Becausing hashing is so important, the Object class has a hashCode method:
int hash = x.hashCode();
19
Q

keySet

A

The collection of unique keys in a HashMap. Java has an inbuilt method for retrieving all keys for a given HashMap, keySet( ), which can then be used to retrieve all corresponding values. For example:
Set keySet = m.keySet( );

for (String key : keySet)

{

Color value = m.get(key);
System.out.println(key + “->” + value);
}

20
Q

How do you iterate through all of the key value pairs in a HashMap?

A

Use a for loop, or enhanced for loop:

for (String key : movieToActors.keySet( ) )

{
 // your code here
}