Java Collections Framework Flashcards
Java collections framework
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.
List
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.
Set
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.
Stack
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.
Queue
In a queue, you add items to one end (the tail) and remove them from the other end (the head).
Priority queue
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.
Map
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.
LinkedList
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.
What is one advantage of a LinkedList compared to an ArrayList?
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.)
What is one drawback of a LinkedList?
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.
What are the most commonly used methods of the LinkedList class?
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.
What is the syntax for declaring a new HashMap?
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!*
Iterator
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.
What is an essential difference between a collection and a set?
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”))
Common methods for working with sets
- .add()
- .remove()
- .contains()