forked from jin-zhe/cheatsheets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava.java
More file actions
253 lines (224 loc) · 9.11 KB
/
Java.java
File metadata and controls
253 lines (224 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* Bit Manipulation */
int complement = ~0; // 32 '1's
<< len // left shift: pads right by len number of '0's
>> len // right shift: pads left by len number of '0's
>>> // right shift: ignores sign extension and pads '0' on the left
num & mask // AND operation
num | mask // OR operation
num ^ mask // XOR operation
/* Note the usual arithmetic shorthands also apply. i.e. &=, |=, ^= */
(~(450)+1) == -450 // take complement then add 1 to toggle between positive and negative
/* Byte manipulation */
/* Note: Bytes in Java are signed by default*/
int unsignedVal = b & 0xFF;
/* Integer */
Integer.parseInt("123")
Integer.toBinaryString(number)
Integer.MAX_VALUE
Integer.MIN_VALUE
/* Math */
Math.max(one, two);
Math.min(one, two);
Math.pow(one, two); // one^two
Math.sqrt(num);
Math.floor(aFloat);
Math.abs(obj.hashCode()); // integer representation of hashCode()
/* Strings */
int length = String.length();
.trim() // remove leading and trailing whitespaces
.replaceAll("\\s+", " "); // remove multiple sequential whitespaces
Integer.toString(10, 2); // parse int to bin string: convert 10 to "1010"
StringBuffer // append(), reverse(), length(), substring(), charAt(), deleteCharAt(), delete(start, end_exclusive)
char[] charArr = str.toCharArray(); // convert string to char array
String str = new String(charArr); // convert char array to string
toLowerCase();
toUpperCase();
/* Characters */
/* Note: characters in Java are UTF-16 encoded so they each occupy 2 bytes in memory */
char two = '0' + 2; // parse int to char: convert 2 to '2'
char two = Character.forDigit(2, 10); // same as above
int ans = '2' - '0'; // parse char to int: convert '2' to 2
/* Random */
Random gen = new Random();
gen.nextInt(x); // returns random number between 0 and x exclusive
/* Arrays */
int length = array.length
subarray = Arrays.copyOfRange(arr, start, end_exclusive); // only after 1.6
System.arraycopy(src_arr, src_start, dest_arr, dest_start, copy_length); // makes copy of array
Arrays.sort(arr); // sorts array in-place
Arrays.toString(arr); // e.g. [1,2,3] => "1, 2, 3"
Arrays.fill(arr, deafault_val); // initializes all cells with the default value given
/* Iterators */
ArrayList<Integer> al = ...
Iterator<Integer> itr = al.iterator();
while(itr.hasNext()) {
Integer curr = itr.next();
// do something with curr
}
/* Comparators: classes for comparison between objects. see http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/ */
/* using anonymous classes */
Comparator<Integer> myComparator = new Comparator<Integer>() {
public int compare(Integer o1, Integer o2){
return o2 - o1; // biggest to smallest (smallest to biggest is o1 - o2)
// return o1.compareTo(o2); // using the class' natural comparator and in ascending order
}
};
/* using actual classes */
class ColorComparator implements Comparator<CarSort> {
public int compare(CarSort c1, CarSort c2) {
return c1.getColor().compareTo(c2.getColor());
}
}
/* Comparable */
Class MyClass implements Comparable<MyClass> {
int value;
...
public int compareTo(MyClass something) {
return this.value - something.value; // ascending order
}
}
/* collections */
/* Sorts collection using custom comparison class */
Collections.sort(list, new Comparator<Integer>(){
public int compare(Integer o1, Integer o2){
return o1 - o2; // ascending (descending is o2 - o1)
// return o1.compareTo(o2); // using the class' natural comparator and in ascending order
}
});
/* Data Structures */
TreeMap<K,V> BST = new TreeMap<K,V>(new Comparator); // BST: put(key, value), get(key), size(), isEmpty()
PriorityQueue<Integer> PQ= new PriorityQueue<Integer>(initial_capacity, myComparator); // PQ: peek(), poll(), offer(item), size(), isEmpty()
LinkedList<Object> list = new LinkedList<Object>(); // Queue: peek(), poll(), offer(item), isEmpty()
Stack<Object> stack = new Stack<Object>(); // Stack: peek(), pop(), push(item), empty()
PriorityQueue<Long>(10, Collections.reverseOrder()); // Max heap
public static class Pair<X,Y>{
X one;
Y two;
public Pair(X one, Y two){
this.one = one;
this.two = two;
}
}
/* Hashmaps */
HashMap<Key_Class, Value_Class> map; // put(key, value), get(), size(), containsKey(key)
map.getOrDefault(key, 0) // Hashmap default values. Note can return null if key exists and its value is null
for (HashMap.Entry<Key_Class, Value_Class> entry: charMap.entrySet()) // iterate through a hashmap
/* Hashset */
HashMap<Key_Class> set; // add(item), remove(object), contains(item), size()
boolean b = setA.containsAll(setB); // check if setB is a susetB of setA
setA.addAll(setB); // union - transform in-place setA into the union of setA and setB
setA.retainAll(setB); // intersection - transforms in-place setA into the intersection of setA and setB
setA.removeAll(setB); // difference - transforms in-place setA into the (asymmetric) set difference of setA and setB.
/* Generics */
/* Wildcard */
public void printArrayList(ArrayList<?> list) {
Iterator<?> itr = list.iterator();
while(itr.hasNext()) {
System.out.print(itr.next() + " ");
}
System.out.println();
}
/* Anonymous classes */
HelloWorld frenchGreeting = new HelloWorld() {
String name = "tout le monde";
public void greet() {
greetSomeone("tout le monde");
}
public void greetSomeone(String someone) {
name = someone;
System.out.println("Salut " + name);
}
};
/* Exceptions */
/* Exception heirarchy:
* [Throwable]
* / \
* [Error] [Exception]
* / \
* [IOException] [RuntimeException]
*/
/* Userful throwable methods */
getMessage(); // Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor.
Throwable cause = getCause(); // Returns the cause of the exception as represented by a Throwable object.
toString(); // Returns the name of the class concatenated with the result of getMessage()
printStackTrace(); // Prints the result of toString() along with the stack trace to System.err, the error output stream
/* Try/Catch block */
try {
//Protected code
}
catch(ExceptionType1 e1) {
//Catch block
}
catch(ExceptionType2 e2) {
//Catch block
}
catch(ExceptionType3 e3) {
//Catch block
}
finally {
//(optional)The finally block always executes, whether or not an exception has occurred
}
/* throws/throw */
public class className {
public void deposit(double amount) throws RemoteException {
// Method implementation
if (condition) {
throw new RemoteException();
}
}
// Remainder of class definition
}
/* user-defined exception classes */
public class InsufficientFundsException extends Exception {
private double amount;
public InsufficientFundsException(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
/* Enum (taken from: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) */
/* without contructor */
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
/* with constructor */
public enum Planet {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6),
EARTH (5.976e+24, 6.37814e6),
MARS (6.421e+23, 3.3972e6),
JUPITER (1.9e+27, 7.1492e7),
SATURN (5.688e+26, 6.0268e7),
URANUS (8.686e+25, 2.5559e7),
NEPTUNE (1.024e+26, 2.4746e7);
private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
private double mass() { return mass; }
private double radius() { return radius; }
// universal gravitational constant (m3 kg-1 s-2)
public static final double G = 6.67300E-11;
double surfaceGravity() {
return G * mass / (radius * radius);
}
double surfaceWeight(double otherMass) {
return otherMass * surfaceGravity();
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java Planet <earth_weight>");
System.exit(-1);
}
double earthWeight = Double.parseDouble(args[0]);
double mass = earthWeight/EARTH.surfaceGravity();
for (Planet p : Planet.values())
System.out.printf("Your weight on %s is %f%n",
p, p.surfaceWeight(mass));
}
}