Java Collections Flashcards
What are Collections?
§ 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 data structures?
Data structures are objects that provide a way to store and organize data to use (access/modify) efficiently.
Which are the most popular interfaces in Java Collections Framework?
SET, LIST, MAP
• Collection is anything holding data called "elements" (of the same type). Nothing more specific is assumed. • List is an indexed collection of data where each element has an index. Something like the array, but more flexible. Data in the list keep the order of insertion. Typical operation: get the n-th element. • Set is a bag of elements, each elements just once (the elements are distinguished using their equals() method. Data in the set are stored mostly just to know what data are there. Typical operation: tell if an element is present in the list. • Map is something like the List, but instead of accessing the elements by their integer index, you access them by their key, which is any object. Like the array in PHP :) Data in Map are searchable by their key. Typical operation: get an element by its ID (where ID is of any type, not only int as in case of List).
What is the use case for “List”?
Lists are used for ordered data collection, where the placement of the object is essential.
What is the use case for “Set”
Sets are used for data collection, where no duplicate objects can be present.
What is the use case for “Queue”
Queues are used for ordered data collection, where operations with the first/last elements are performed.
What is the use case for “Map”?
Maps are used for data collection as key-value pairs.
What is the use case for “Priority Queue”?
Priority queues are used for ordered data collection, where objects are processed based on a defined ‘priority’ using comparators.
Please note that PriorityQueue is a class in Java Collections Framework, not an interface. The aim of this question is to demonstrate understanding of the Heap data structure.
How is “Map” interface different from most of the other JCF (Java Collections Framework) interfaces?
Other JCF interfaces store collections of single objects and implement the ‘Collection’ interface, but ‘Map’ stores object key-value pairs.
Compare the differences of ARRAY, ARRAYLIST and LINKEDLIST.
a. Array is a fixed size collection, where you have to define the size at creation (where a memory space of that size is reserved), possible to store primitives, which is not the case for JCF collections.
b. ArrayList is an implementation of List interface, has some initial capacity which changes dynamically. The main advantage is speed when accessing objects by the index for large volumes.
c. LinkedList is an implementation of List and Queue interface, has no initial capacity as all objects are stored as nodes (objects that have information on previous and next element). Main advantages are when the collection is frequently changing, and functionality of queue is needed (working with first/last elements).
What do these abbreviations mean in the context of data structures FIFO, FILO?
○ FIFO is an abbreviation for “First in First out”. It is a method for handling data structures where the first element is processed first and the last element processed last.
–> QUEUE (like a line to a rollercoaster or postal office, or a printer)
○ FILO is an abbreviation for “First in Last out”. It is a method for handling data structures where the first inserted element is processed last, and the most recently inserted element is processed first.
–> STACK (like a stack of dishes in the sink)
What is the “Big-O” notation?
Big-O notation describes how fast an algorithm is in terms of the number of operations it makes. It shows how the number of operations grows as you add more items for the algorithm to process.
What’s the efficiency of inserting an element in a random place in LinkedList?
Inserting an element at a random location in a linked list only requires to update the pointers of the element before it and after it.
As such, it is an efficient linear-time operation - 0(1).
What’s the efficiency of accessing a random element place in LinkedList?
Elements in a linkedlist do not have a linear location in memory. The algorithm must go through the list until finding the desired element’s location to access it. It can be very inefficient if the location isn’t at the start of the list, as the algorithm requires traversing many elements. At worst, it’s a linear-time operation - O(n).
What’s the efficiency of accessing a random element in an ArrayList?
ArrayLists are backed by arrays. Accessing a random location is efficient because each item has a known offset in memory. It’s a constant-time operation - O(1).