Java cheasheet
Simple pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Maven
mvn exec:java -Dexec.mainClass="Main" # => Run java Main.java from maven.
System.out.printf("%3d ", n); // print number with spaces.
System.out.println(Files.readAllLines(Paths.get("file.txt"))); // Read file with java.
Arrays.copyOfRange([1,2,3], 1, [1,2,3].length); // subarray from 1 to length array
Initialize 2 dim array
int[][] input = new int[][] {
{ 1, 2, 3, 4 },
{ 2, 2, 3, 2 }
};
print two dim array
java.util.Arrays.deepToString(table)
Read stdin
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
spaced delimeted string to stream
import java.util.stream.*;
import java.util.regex.*;
Stream stream = Pattern.compile(" ").splitAsStream(scanner.nextLine()); // O(1)
// or simpler but would read the string completely and split to array // O(2)
Arrays.stream(sc.nextLine().split(" ")).forEach(item -> {});
// calculate total sum with streams.
int totalSum = Arrays.stream(lineAsStr.split(" "))
.map(numAsStr -> Integer.valueOf(numAsStr))
.reduce(0, (sum, n) -> sum += n);
Ordered Tree Map
As it sounds it’s a map so you put things but you can traverse keys in ordered way.
TreeMap treeMap = new TreeMap();
treeMap.put("a", "b");
treeMap.put("d", "d");
treeMap.put("c", "c");
Iterator it = treeMap.entrySet().iterator();
while (it.hasNext()) {
System.out.println(it.next()); // Printed by key order.
}
Resources