Comparable and Comparator in Java
Comparable gives a class one natural order. Comparator supplies any number of orders from outside, without touching the class.
Interview preparation material.
165 notes tagged
Comparable gives a class one natural order. Comparator supplies any number of orders from outside, without touching the class.
An Iterator walks a collection one element at a time, and it is the only safe way to remove during a traversal.
Buckets, hashing, collisions and resizing. Understanding the mechanism explains the performance and every rule about keys.
A Map stores key to value pairs with unique keys, and it is the collection you will reach for most often.
A List keeps insertion order and allows duplicates. ArrayList is the default; LinkedList wins only at the ends.
throw raises an exception now; throws declares that a method may raise one and leaves the handling to the caller.
Errors, checked exceptions and unchecked exceptions sit in one hierarchy, and knowing which is which decides how you should react.
Wrapper classes turn primitives into objects so they can be used with generics and collections, and the conversion is automatic but not free.
Two objects that are equal must have the same hash code. Breaking that contract quietly breaks every hash based collection.
Abstraction exposes what a type does and hides how. Abstract classes share partial implementation, interfaces declare a capability.
One reference type, many possible behaviours. Polymorphism is what lets code work with a supertype and stay correct as new subtypes appear.
A subclass replaces inherited behaviour by declaring the same signature, and the JVM chooses the version at runtime from the actual object.