Prep: Learning Java

OK so for my interview, this article persuaded me that I need to learn Java again. I’m amazed I made it this far.

http://javarevisited.blogspot.com/2011/04/top-20-core-java-interview-questions.html

http://bateru.com/news/2011/03/484

OOP
+ three main principals of oops are called Polymorphism, Inheritance and Encapsulation
+ Polymorphism exists in three distinct forms in Java:
+ Method overloading
+ Method overriding through inheritance
+ Method overriding through the Java interface

Objects
+ always implement equals and hashCode together
+ if two objects are equal, hashcodes must be equal (but not necessarily the other way around)
+ or… if two object’s hashcodes are not equal, the objects cannot be equal
+ Comparable or Comparator must be equivalent with equals() or else bugs will emerge in Tree and Hash collections
+ in equals, instanceof allows subclasses to equal, while getClass() == getClass() only allows the specific class
+ subclasses can get in trouble with the hashcode
+ super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass’s constructor
+ won’t prevent garbage collection: WeakReference weakWidget = new WeakReference(widget);
+ also WeakHashMap won’t prevent garbage collection of its values

Primitives
+ 8 primitive types are byte, char, short, int, long, float, double, and boolean

Strings
+ immutable and final
+ for concurrency and security (notably network operations, class loading)
+ literal strings go into thread pool
+ new String() goes into heap, not the String pool
+ pool is in permgen memory
+ String.intern() puts a string in heap into the String pool
+ substring keeps a copy of the original string! trim it with new String(): new String(veryLongString.substring(1,10));

Executor
+ ThreadPoolExecutor.execute() runs a runnable
+ ScheduledThreadPoolExecutor.submit() returns a Future for a Callable
+ Future.get will rethrow the exception, wrapped in an ExecutionException

Collections Javadocs

BitSet
+ vector of bits that grows as needed

Collection
+ implementations should have 2 constructors: void (no-arg) and Collection
+ throw UnsupportedOperationException if method in implementation doesn’t apply

Collections
+ addAll(collection, a,b,c…)
+ asLifoQueue(Deque)
+ binarySearch
+ checked…(coll, class, class?) dynamically type-safe view of collection
+ copy(List,List)
+ disjoint(Coll,Coll) true if the two specified collections have no elements in common
+ empty…
+ enumeration(Coll)
+ fill(List, object)
+ frequency(Coll, Object) number of elements in the specified collection equal to the specified object
+ shuffle(List) shuffle(List,Random)
+ reverse
+ rotate
+ singleton…
+ sort
+ swap(List,i,j)
+ synchronized..
+ unmodifiable

Comparator
+ consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 in S
+ if not consistent with equals, sorted sets and maps will behave strangely
+ good idea for comparators to also implement java.io.Serializable, as they may be used as ordering methods in serializable data structures

Dequeue
+ “deck”
+ queue or de-queue at either end
+ added in 1.6
+ This interface should be used in preference to the legacy Stack class
+ ArrayDeque, LinkedBlockingDeque, LinkedList
+ insert, remove, examine
+ offer, poll, peek
+ no indexed access

Formattable
+ formatTo(Formatter formatter, int flags, int width, int precision)

HashMap
+ accepts null for keys and values
+ key’s hash determines bucket
+ bucket contains a linked list of key,value pairs
+ key.equals() is used for all key comparisons (get, update, remove)
+ load factor determines when to rehash
+ HashMap accepts null values
+ not synchronized
+ if no equals or hashcode, just uses object identity (memory location)
+ a bad hashCode method will result in more frequent collisions & poor performance when hashing
+ constant time get and put, as long as the hash function distributes equally, with amortized rehashing
+ with a bad hash, turns into a big linked list
+ load factor default = .75
+ when over load factor, rehash
+ don’t need to synchronize the map for updates to values in the map
+ can wrap with Collections.synchronizedMap
+ iterators fail-fast (best effort!): structural change breaks all existing iterators

List
+ Note: While it is permissible for lists to contain themselves as elements, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a list.

ListIterator
+ An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator’s current position in the list

Map
+ great care must be exercised if mutable objects are used as map keys
+ it is not permissible for a map to contain itself as a key
+ While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a map

NavigableMap
+ A SortedMap extended with navigation methods returning the closest matches for given search targets
+ ConcurrentSkipListMap, TreeMap
+ ceiling descent first last higher lower head tail sub

NavigableSet
+ A SortedSet extended with navigation methods reporting closest matches for given search targets
+ ConcurrentSkipListSet, TreeSet

PriorityQueue
+ unbounded priority queue based on a priority heap
+ this implementation provides O(log(n)) time for the enqueing and dequeing methods (offer, poll, remove() and add)
+ linear time for the remove(Object) and contains(Object) methods
+ and constant time for the retrieval methods (peek, element, and size)

Queue
+ BlockingDeque, BlockingQueue, Deque
+ AbstractQueue, ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, LinkedList, PriorityBlockingQueue, PriorityQueue, SynchronousQueue

RandomAccess
+ Marker interface used by List implementations to indicate that they support fast (generally constant time) random access

Set
+ Great care must be exercised if mutable objects are used as set elements

TreeMap
+ A Red-Black tree based NavigableMap implementation
+ guaranteed log(n) time cost for the containsKey, get, put and remove operations
+ behavior of a sorted map is well-defined even if its ordering is inconsistent with equals

Concurrency Javadocs

Singletons
+ blech
+ multiples: VMs, classloaders, App Engine unload/load

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s