Week 12 Flashcards
What is the purpose of the collections framework in Java?
Provides a set of interfaces and classes that allow developers to use collections of objects in an organized /efficient way
Java collections framework: Common operations
Add, remove, search for elements, iterate/sort elems, and perform opeartions like union, intersection, and difference
Benefits of the JCF
Productivity, readability, maintainability, fewer bugs, greater performance, generic programming
What is the collection interface in Java?
The root interface of the JCF.
Defines common methods,
Represents a group of objects called “elements”
Provides methods for adding/removing/querying elements in the collection
What are the 4 types of collections interfaces?
List, Set, Queue, and Map
List, Set, Queue, and Map. What do they represent?
List - Ordered collection of elements
Set - Collection of unique elements
Queue - Collection of elements with a specific ordering
Map - Represents a collection of key-value pairs
Doe the “List” interface allow duplicate elements? How about the Set interface?
List allows duplicate elements, Set does not
What does peek() do?
Allows you to view the top element on the stack without removing it.
What sort of data structure style does a stack in Java follow?
Last in first out (Elements added to the top, removed from the top)
Queue vs Stack
Queue = First in first out (Elements added tot he rear end, removed from the front)
Does a queue provide access to elements in the middle?
No
What does the map interface in Java represent?
A collection of key-value pairs. Each key is unique and maps to a corresponding value.
Allows elements to be accessed by using their key rather than their index or value
add(), remove(), contains(), size(), isEmpty(), iterator(), toArray()… What do these methods have in common?
These methods are available in the Java Collection Interface
What is the purpose of the diamond syntax? < >
It specifies generic types when creating objects of generic classes. Allows the compiler to infer the type based on the context. Improves readability.
// Before Java 7
List<String> myList = new ArrayList<String>();</String></String>
// With diamond syntax in Java 7 and later
List<String> myList = new ArrayList<>();</String>
5 characteristics of linked lists
- Dynamic size (grow or shrink at any time)
- Efficient insertion or deletion at ends of list
- Do not require contiguous memory (unlike arrays) more efficient for memory
- Bad: Variable access time, needs to traverse each node from the beginning until the desired node is found
- Bad: Do not support random access to elements (unlike arrays), the items must be accessed sequentially from head or tail of list