Collections/Genetics Flashcards
What are collections in Java?
They are a framework that provides an architecture to store and manipulate a group of objects.
What are the interfaces in the Collections API
Set, List, Queue, Deque
What is the difference between a Set and a List?
A set is unordered and cant have duplicates, while a List is ordered and can contain duplicates
What is the difference between an Array and an ArrayList?
An array is an object that has elements of similar data types with a set size. An ArrayList provides a dynamically sized array
What is the difference between ArrayList and Vector?
They both use Arrays internally as a data structure and resize dynamically, but a Vector doubles in size when it is resized whereas an ArrayList increases by half of its size
What is the difference between TreeSet and HashSet?
HashSet does not maintain any order in the set and are inserted based on their hashcode, TreeSet is sorted by ascending order by default
What is the difference between HashTable and HashMap?
HashMap is non-synchronized, not thread safe, and cannot be shared between threads without proper synchronization, whereas HashTable is synchronized and thread safe. HashMap allows one null key and multiple null values, whereas HashTable doesnt allow any null keys or values
Are Maps in the Collections API? What makes Map different from other interfaces?
Map is not part of the Collections API. Maps store key value pairs while other interfaces store single values
List several ways to iterate over a Collection. How would you iterate over a map?
We can use while loops, for loops, enhanced for loops, the forEach() method, or using an iterator
We cannot iterate a map directly with an iterator. You can iterate over Map.entrySet() using a for-each loop, Using keySet() and values() methods, using iterators over Map.Entry, Iterating over keys and searching for values
What is the purpose of the Iterable interface? What about Iterator?
The iterable interface is the root interface for all collection classes. It provides the ability to iterate the elements in a forward direction only
An iterator is an object that can be used to loop through collections like ArrayList and HashSet
What is the difference between the Comparable and Comparator interfaces?
The Comparator interface sorts collections using two objects provided to it, whereas the Comparable interface compares “this” which refers to one of the objects provided to it
What are generics? What is the diamond operator <>?
Generics means parameterized types, which allows a type to be a parameter for a method, class, or interface. The diamond operator simplifies the use of generics when creating an object