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
This collection of elements does not allow duplicates and does not maintain an order, what is it?
A set
Advantages of Set vs. List
Optimized organization of values for efficiency
Faster insert/removal of elems
Eliminates duplicates (efficient)
What are the implementing classes of the set interface?
HashSet (based on a hash table) and TreeSet (based on a binary search tree)
What is the purpose of the hashCode() method in a hash table?
Used to infer the hash code of an element, which is used to group together similar elements in the hash table
What does the equals() method do? How is it used?
Determines if 2 elements in a hash table are equal. It’s used to ensure there are no duplicates allowed in the set
What classes can be used in a hash set in Java?
Wrapper classes - String, Integer, Double, Point, Rectangle, Color
This interface is used to define a natural order for objects, which is used for elements in TreeSet–where elements are kept in a sorted order
Comparable
When do you use a TreeSet over a HashSet?
Use a TreeSet when you want to visit the sets elements in a sorted order. Otherwise, HashSet is more efficient.
How can you declare/instantiate a set in Java?
Use the set interface
Set<String> name = new HashSet<>();
or
Set<String> name = new TreeSet<>();</String></String>
A duplicate element is added to a set, what happens? When you try to remove an element that is not in a set, what happens?
Nothing for both. they’re ignored and not added/removed.
Iterator<String> iter = names.iterator();
while (iter.hasNext()) {
String name = iter.next();
// Do something with name
}</String>
What does the above code do?
Iterates over each element in the “names” set
Can you add an element to a set at an iterator position? How about remove?
You cannot add any elements, but you can remove them using “remove()”
Maps. What is their purpose?
Associate keys with values, you can then sue the key to lookup values
What is the purpose of this <String, HashSet<String>> ?</String>
Associates a String with a set of strings, like a thesaurus that lists synonyms for a given word
Queues follow what order:
FIFO
How are queues typically applied?
Print queues, print jobs are added to the queue and printed in the order they are added
How do you initialize a queue? Where does it come from?
Queue<String> q = new LinkedList<>();</String>
Comes from the LinkedList class
Can you use strings as elements in a priority queue?
Yes, as the elements are retrieved according to their priority, which can be determined by the natural order off the strings
What advantage is there to declaring something a queue, rather than a linked list?
A queue object can only have queue operations invoked on it, maintains the integrity of the queue data structure
ArrayLists to implement queues: yes or no?
No. Inefficient. Adding or removing elements at the head of the queue requires moving all other elements in the list