Collection Flashcards
(20 cards)
Give a short description of the interface: Iterable
a class implementing this interface can be used for iterating with a foreach statement
Give a short description of the interface : Collection
Common base interface for classes in the collection hierarchy. When you want to write methods that are very general, you can pass Collection interface.
Most important methods: boolean add(Element elem) void clear() boolean isEmpty() Iterator iterator() boolean remove(Object obj) int size() Object[] toArray()
Give a short description of the interface : List
- Base interface for containers that store a sequence of elements.
- You can access the elements using an index.
Maintains insertion order - Can store duplicates
Give a short description of the interface : Set, SortedSet, NavigableSet
- interfaces for containers that dont allow duplicates
- SortedSet maintains the set elements in a sorted order
- NavigableSet allows searching the set for the closest match
Give a short description of the interface : Map, SortedMap, NavigableMap
interfaces for containers that map keys to values. In sortedMap, the keys are sorted in order
- NavigableMap allows you to search and return the colest match for given search
- Map hierarchy does NOT extend Collection interface
Give a short description of the interface: Queue, Deque
Queue interface is a base interface for containers that hold a sequence of elements for processing.
For ex. the classes implementing Queue can be LIFO or FIFO
* In a deque you can insert or remove elements from both ends
Give a short description of the interface: Iteratir, ListIterator
- You can transverse over the container in the foward direction if a class implements the Iterator interface
- You can traverse in both foward and reverse direction if a class implements the ListIterator interface
Which are the import concrete classes in the Collection Framework?
- ArrayList
- LinkedList
- HashSet
- TreeSet
- HashMap
- TreeMap
- PriorityQueue
Give a brief description of the concrete class ArrayList
- Implemented as a resizable array
- Fast to serach
- Slow to delete/insert
- allow dups
Give a brief description of the concrete class LinkedList
- Doubly linked list data strut
- Fast to insert or delete elements
- slow for searching elements
- can be used when you need stack (LIFO) or Queue(FIFO)
- allow dups
Give a brief description of the concrete class HashSet
- hash table data structure
- does not allow dups
- fast for earching and retrieving elements
- does NOT maintain any order
Give a brief description of the concrete class TreeSet
- red-black tree data structure
- does not allow dup
- store elements in a sorted order
Give a brief description of the concrete class HashMap
- hash-table data structure
- key values pair
- uses hashing for finding a place to search or store a pair
- searching or inserting is very fast
- not store elements in order
Give a brief description of the concrete class TreeMap
- red-black tree data structure
- store elements in a sorted order
Give a brief description of the concrete class PriorityQueue
- heap data structure
- retrieving based on priority
- irrespective of the order in which you insert
Given a list:
ArrayList{String} languageList = new ArrayList{}()
how to iterate over the list using iterator?
for(Iterator{String} languageIter = languageList.iterator(); languageIter.hasNext();) {
String language = languageIter.next();
System.out.println(language);
}
What is the difference between ArrayList and ArrayDeque?
The difference is that you can add an element anywhere in a array list using an index; however, you can add an element only either at the front or end of the array deque.
That makes insertion in array deque more efficient than array list; however, navigation in an array deque becomes more expensive than in array list
What is the method the Comparable interface has?
int compareTo(Element that)
Regards to comparings its, what should the int value be?
1 if current object more than passed object
0 if equal
-1 current object less than passed object
Of all the java.util package classes, which ones are thread-safe?
Only Vector and Hashtable
java.util.Collections class contains a synchronizedCollection method that creates thread-safe instances based on collections which are not. For example: Set s = Collections.synchronizedSet(new HashSet());