Java API Flashcards
API offered by Queue Interface
add(E element) - insert element into queue if possible (else exception)
E element() - retrive but not remove the head (exception wheen empty)
boolean offer(E) - add if possible (else return false)
E peek() - same as element(), but returns null when empty
E poll() - similar to peek, but removes the element
E remove() - similar to poll, but throws exception when empty
API offered by the Deque interface
addFirst(E)/addLast(E) - add element if possible, otherwise exception
offerFirst(E)/offerLast(E) - add element if possible, otherwise false
E removeFirst()/removeLast() - remove element, otherwise exception if empty
E pollFirst()/pollLast() - retrive and remove element or null
E peekFirst()/peekLast() - retrive element or null
E getFirst()/getLast() - retrieve element or exception
What are the Stack equivalent methods of a Deque?
push(E) -> addFirst(E)
E pop() -> E removeFirst()
E peek() -> E peekFirst()
Which class should be used instead of a Stack and why?
A Deque (a LinnkedList for example) as it is more “complete” and “consistent”, and a Stack is an extension of the Vector class, which is not recommended when used in a non-concurrent environment.
Can you sub-class a final class in java?
No, it is not allowed.
How can you prevent a class from being subclassed without declaring it final?
Make its constructor(s) private, thus subclasses won’t be able to inherit from it.
When is the finalize method called in java?
Before the class gets garbage collected.
PriorityQueue class API
add(E) - same as offer
clear()
offer(E)
E peek() - retrive but does not revome or null
E poll() - retrive and remove or null
T[] toArray(T[]) - convert to an array
Can you iterate over a PriorityQueue using an Iterator to retrive elements in ascending or descending order?
No. It supports iterator, but its ordering is not guaranteed or sorted. If you need to access the elements in a specific order, then you have to convert it to an array usiing toArray(T[]) and then sort the array using Arrays.sort()
Describe some of the Scanner API methods.
boolean hasNext() - are there more tokens?
boolean hasNextInt()/hasNextBoolean()/… - next token can be interpreted as int/boolean/…
next()/nextLine()/nextInt()/nextBoolean()/… - read next token as String/int/boolean/etc
Lock interface API
void lock() - acquires lock
Condition newCondition() - return a new condition bound to this lock
tryLock() -
tryLock(long time, TimeUnit unit) - trys to lock
unlock() - releases this lock
ReadWriteLock interface API
Lock readLock() - obtains a read lock
Lock writeLock() - obtains a write lock
Which classes implement the Lock interface?
ReentrantLock
ReentrantReadWriteLock
Semaphore class API
Semaphore(int permits) / Semaphore() - constructor
acquire() - acquire a permit or wait until one is available
acquire(int permits) - acquire more than one permit
release()/release(int permits) - releases a permit
boolean tryAcquire()/tryAcquire(int permits) - tries to acquire pemits (all or nothing)
CountdownLatch API
CountdownLatch(int count) - constructor
await()/await(int time, TimeUnit units) - waits for counter to reach zero
countDown() - decrements the count of the latch