The document is a comprehensive cheat sheet for the Java programming language, covering fundamental concepts such as data types, control structures (if statements, loops), operators, arrays, object-oriented programming principles (inheritance, polymorphism), and Java collections. It includes code snippets and examples for each concept, making it a useful reference for Java developers. Additionally, it provides insights into generics and common collection classes like ArrayList, LinkedList, and HashMap.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views
Java Cheat Sheet
The document is a comprehensive cheat sheet for the Java programming language, covering fundamental concepts such as data types, control structures (if statements, loops), operators, arrays, object-oriented programming principles (inheritance, polymorphism), and Java collections. It includes code snippets and examples for each concept, making it a useful reference for Java developers. Additionally, it provides insights into generics and common collection classes like ArrayList, LinkedList, and HashMap.
Single Inheritance with “extends” List<T>: Similar to arrays A queue that is always automatically sorted class A{ } ArrayList<T>: Slow insert into middle using the comparable function of an object class B extends A{ } //ArrayList has fast random access abstract class C { } LinkedList<T>: slow random access public static void main(String[] args) { class D extends C { } //LinkedList fast as queue/stack Comparator<String> cmp= new LenCmp(); class E extends D Stack: Removes and adds from end PriorityQueue<String> queue = Abstract methods new PriorityQueue<String>(10, cmp); abstract class F { List Usage: queue.add("short"); abstract int bla(); boolean add(T e); queue.add("very long indeed"); } void clear(); //empties queue.add("medium"); class G extends F { boolean contains(Object o); while (queue.size() != 0) int bla() { //required method T get(int index); System.out.println(queue.remove()); return 5; T remove(int index); } } boolean remove(Object o); class LenCmp implements Comparator<String> { } //remove uses comparator public int compare(String x, String y){ Multiple Inheritance of interfaces with T set(int index, E val); return x.length() – y.length(); “implements” (fields not inherited) Int size(); } interface H { } void methodA(); List Traversal: boolean methodB(int arg); for(int i=0i<x.size();i++) { java.util.Collections algorithms } //use x.get(i); Sort Example: interface I extends H{ } //Assuming List<T> x void methodC(); Collections.sort(x); //sorts with comparator } //Assuming List<T>: Sort Using Comparator: interface K {} for(T e : x) { Collections.sort(x, new Comparator<T>{ class J extends F implements I, K { //use e public int compareTo(T a, T b) { int bla() { return 5; } //required from F } //calculate which is first void methodA(){} //required from H //return -1, 0, or 1 for order: boolean methodB(int a) { //req from A Queue<T>: Remove end, Insert beginning return someint; return 1; LinkedList implements Queue } } } void methodC(){} //required from I Queue Usage: Example of two dimensional array sort: } T element(); // does not remove public static void main(final String[] a){ Type inference: boolean offer(T o); //adds final String[][] data = new String[][] { A x = new B(); //OK T peek(); //pike element new String[] { "20090725", "A" }, B y = new A(); //Not OK T poll(); //removes new String[] { "20090726", "B" }, C z = new C(); //Cannot instantiate abstract T remove(); //like poll new String[] { "20090727", "C" }, //Method calls care about right hand type Traversal: for(T e : x) {} new String[] { "20090728", "D" } }; (the instantiated object) Set<T>: uses Comparable<T> for uniqueness Arrays.sort(data, //Compiler checks depend on left hand type TreeSet<T>, items are sorted new Comparator<String[]>() { HashSet<T>, not sorted, no order public int compare(final String[] GENERICS: LinkedHashSet<T>, ordered by insert entry1, final String[] entry2) { class MyClass<T> { Usage like list: add, remove, size final String time1 = entry1[0]; T value; Traversal: for(T e : x) {} final String time2 = entry2[0]; T getValue() { return value; } Map<K,V>: Pairs where keys are unique return time1.compareTo(time2); } HashMap<K,V>, no order } class ExampleTwo<A,B> { LinkedHashMap<K,V> ordered by insert }); A x; TreeMap<K,V> sorted by keys B y; for (final String[] s : data) { } V get(K key); System.out.println(s[0]+" "+s[1]); class ExampleThree<A extends List<B>,B> { Set<K> keySet(); //set of keys } A list; V put(K key, V value); } B head; V remove(K key); } } Int size(); More collections static methods: //Note the extends keyword here applies as Collection<V> values(); //all values Collections.max( … ); //returns maximum well to interfaces, so A can be an interface Collections.min( … ); //returns maximum that extends List<B> Traversal: for-each w/ keyset/values Collections.copy( A, B); //A list into B Collections.reverse( A ); //if A is list Suresh Bishnoi LinkedIn: https://www.linkedin.com/in/bishnoisuresh/