Lecture 8B Flashcards
In Java we must wrap functions in objects to pass them around. How is this realized?
Use interfaces with just a single abstract method. And of course classes which implement this interface. Note: a functor can be realized using an anonymous class (a class without s name).
Do you require generics to use functors?
No, but it increases type safety.
Arrays are only efficient for…
Random access and if the length of the collection does not need to change, no insertion and deletion.
Which package is ye collections framework in?
java.util.
Collections are similar to arrays but more powerful and flexible (albeit slower if dynamic size not needed). Can collections store primitives?
No. Try need to be boxed and unboxed using a wrapper class.
What are basic operations derived from collection interface?
Add(e), remove(e), clear(), contains(e), size()
What are four interfaces that extend interface collection?
Set, List, Queue, Map
What is a Set?
Extends interface Collection. Does not contain duplicate elements. Elements in no particular order. However there are also ordered sets.
Three types of Set:
HashSet (fast search), LinkedHashSet (maintains order, slower), TreeSet (sorts elements using comparable interface).
All classes in the collections Api are generic, but can be used non-generically.
Ok
Charachteristics of Lists?
Allow duplicates, always maintain order (the order in which they are added). Dynamic size. ArrayList stores linear sequence, LinkedList stores by connecting pointers, not a continuous block of memory.
When to use Arraylist versus LinkedList?
If only need to add element at end of list-ArrayList. Also speedy, random access. If need to insert or remove elements form other positions then LinkedList. Slower.
Explain Vector and Stack?
Vector like ArrayList but thread safe. Stack derived from Vector and allows last-in-first-out LIFO methods: pop(), push(e), peek()
LinkedList also implements queues interface. Which allows…
FIFO.
What are maps?
They allow to associate keys with values - keys can be objects not just numbers.