Collections / Generics Flashcards
What are collections in Java?
The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.
Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.
Java Collection means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).
What are the interfaces in the Collections API?
The collection interfaces are divided into two groups. The most basic interface, java.util.Collection, has the following descendants:
java.util.Set
java.util.SortedSet
java.util.NavigableSet
java.util.Queue
java.util.concurrent.BlockingQueue
java.util.concurrent.TransferQueue
java.util.Deque
java.util.concurrent.BlockingDeque
The other collection interfaces are based on java.util.Map and are not true collections. However, these interfaces contain collection-view operations, which enable them to be manipulated as collections. Map has the following offspring:
java.util.SortedMap
java.util.NavigableMap
java.util.concurrent.ConcurrentMap
java.util.concurrent.ConcurrentNavigableMap
What is the difference between a Set and a List?
List is a type of ordered collection that maintains the elements in insertion order while Set is a type of unordered collection so elements are not maintained any order
What is the difference between an Array and an ArrayList?
The array is a specified-length data structure whereas ArrayList is a variable-length Collection class.
What is the difference between ArrayList and Vector?
Vector is synchronized, which means only one thread at a time can access the code, while ArrayList is not synchronized, which means multiple threads can work on ArrayList at the same time.
What is the difference between TreeSet and HashSet?
A TreeSet is a set where the elements are sorted. A HashSet is a set where the elements are not sorted or ordered
What is the difference between HashTable and HashMap?
HashMap allows one null key and multiple null values whereas Hashtable doesn’t allow any null key or value
Are Maps in the Collections API?
The Java platform contains three general-purpose Map implementations: HashMap, TreeMap, and LinkedHashMap
What makes Map different from other interfaces?
The Set interface provides an unordered collection of unique objects, i.e. Set doesn’t allow duplicates, while Map provides a data structure based on key-value pair and hashing.
List several ways to iterate over a Collection.
There are three common ways to iterate through a Collection in Java using either while(), for() or for-each()
How would you iterate over a Map?
five ways of iterating over a Map in Java
1.
Map.entrySet() method
2.
keySet() and values() methods
3.
Map.Entry<K, V> method
4.
forEach(action) method
5.
Iterating over keys and searching for values (inefficient)
What is the purpose of the Iterable interface?
allows an object to be the target of enhanced for loop(for-each loop)
What is the purpose of the Iterator interface?
allows us to access elements of the collection and is used to iterate over the elements in the collection(Map, List or Set)
What is the difference between the Comparable and Comparator interfaces?
While Comparable is used on objects that are naturally ordered, the Comparator interface implements sorting by taking the attributes of an object into consideration. Further, Comparator sorting takes into account objects of two different classes and Comparable compares objects using the “this” reference.
What are generics?
generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods