Java - Collections / Generics
What is the difference between an ArrayList and a Vector?
(1) is NOT synchronized (not thread safe) while (2) is. Also (1) increments by 50% of its size while (2) doubles if it needs more space. (1) is faster since its not synchronized. (2) can use enumeration and iterator for traversal while (1) can only use iterator.
What is the difference between Comparable and Comparator interfaces?
(1) is used to compare itself to another object that implements the (1) interface and override the compareTo() method. This is used for natural ordering. (2) is external to the element type we compare and is separate from the class. It takes advantage of sort() and invokes compare() to sort objects.
What is the difference between a TreeSet and a HashSet
(2) has no order to the elements while (1) will order them. (2) is faster since it implements a special table while (1) takes advantage of a certain structure but is O(log n). (2) allows null objects but (1) does not allow null objects and will throw a NullPointerException. (1) uses compareTo() while (2) uses equals()
What are collections in Java?
A group of objects that can be represented as a single unit. This also belongs to its own (1) framework which contains classes and interfaces.
What is the difference between an Array and an ArrayList?
An (1) pre-allocates space in memory, making it fixed size. A (2) can be dynamically sized. Also (1) can support primitive and objects while (2) only supports objects.
What is the difference between HashTable and HashMap?
Both will store key/value pairs in a hashtable. When we use a key the key is hashed and the code is used to index the value of the table. (1) is synchronized but (2) is not synchronized. (2) allows for one null key and null values while (1) doesn't allow either. (2) is preferred since its faster and using (1) is ONLY for when using threads. (1) can't use null keys since it needs to use hashCode to create the key which can't be done on null. (2) Has the ability to get around this.
What are the interfaces in the collections API?
For Collection (which is an interface): - Queue, Deque - List - Set, sorted set, tree set - Hash Set - Linked Hash Set For Map (also an interface): - SortedMap - NavigableMap
What is the difference between a Set and a List?
Set stores elements in an unordered way but does not contain duplicate elements, whereas list stores elements in an ordered way but may contain duplicate elements.
What are the two root interfaces in the collections API?
The collection interface and the map interface.
Are Maps in the Collections API? What makes a map different from other interfaces?
These ARE in the collections API even though they are a collection framework. The map interface employs some sort of Key/Value mapping to store data.
What are generics? What is the diamond operator(<>)?
This is a parameterized type and allows any type to be passed through in methods, classes, and interfaces. The <> are used to specify the generic type (or the type you are implementing. It cannot be a primitive type).
List several ways to iterate over a collection. How would you iterate over a map?
To iterate through (1) objects you can... 1. Use an enchanced for loop. 2. Use an iterator method. 3. Use a simple for loop. 4. Using forEch method. For (2) objects... 1. Iterate over the entrySet using a for each loop. 2. Iterate throuhg the keySet() or values(). 3. Iterator over the Map.entry<K,V> 4. Using forEach().