Created
February 26, 2020 22:30
-
-
Save lights0123/9b13ab3769f7d980e0edf96a3bcba44b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.stream.Collectors; | |
public class RelearnJava { | |
public static void main(String[] args) { | |
{ | |
Scanner scanner = new Scanner(System.in); | |
String s = scanner.next(); | |
int a = scanner.nextInt(); | |
long l = scanner.nextLong(); | |
float f = scanner.nextFloat(); | |
double d = scanner.nextDouble(); | |
} | |
{ | |
int[] preMade = {2, 3, 1}; | |
// 1 2 3 | |
Arrays.sort(preMade); | |
// Only if array is not a primitive type | |
// Arrays.sort(preMade, (a, b) -> b - a); | |
// Initialized to 0 by default | |
int[] manual = new int[5]; | |
for (int value : preMade) { | |
// Modifying `value` won't work, use second way below | |
System.out.println(value); | |
} | |
for (int i = 0; i < preMade.length; i++) { | |
System.out.println("preMade[" + i + "] = " + preMade[i]); | |
} | |
} | |
{ | |
// 5 rows, 4 columns | |
int[][] matrix = new int[5][4]; | |
/* Prints | |
0 0 0 0 | |
0 0 0 0 | |
0 0 0 0 | |
0 0 0 0 | |
0 0 0 0 | |
*/ | |
for (int[] ints : matrix) { | |
for (int anInt : ints) { | |
System.out.print(anInt); | |
System.out.print(' '); | |
} | |
System.out.println(); | |
} | |
} | |
{ | |
// jagged array: 5 rows, n columns | |
int[][] matrix = new int[5][]; | |
for (int i = 0; i < matrix.length; i++) { | |
matrix[i] = new int[i + 1]; | |
} | |
/* Prints | |
0 | |
0 0 | |
0 0 0 | |
0 0 0 0 | |
0 0 0 0 0 | |
*/ | |
for (int[] ints : matrix) { | |
for (int anInt : ints) { | |
System.out.print(anInt); | |
System.out.print(' '); | |
} | |
System.out.println(); | |
} | |
} | |
{ | |
// Can't use primitive types | |
List<Integer> list = new ArrayList<Integer>(); | |
// Can leave off parameter on right side | |
List<Integer> list2 = new ArrayList<>(); | |
list.add(5); | |
list.add(4); | |
list.add(3); | |
// .get(i) instead of [i] | |
assert list.get(1) == 4; | |
// .set(i) instead of [i] | |
list.set(2, 6); | |
// inserts 2 at index 1 and pushes right | |
list.add(1, 2); | |
// 2 4 5 6 | |
Collections.sort(list); | |
// Custom sort function | |
// 6 5 4 2 | |
list.sort((a, b) -> b - a); | |
for (Integer value : list) { | |
System.out.println(value); | |
} | |
for (int i = 0; i < list.size(); i++) { | |
System.out.println("list.get(" + i + ") = " + list.get(i)); | |
} | |
} | |
{ | |
String s = "bruh moment"; | |
assert s.length() == 11; | |
assert !s.equals("not a bruh moment"); | |
assert s.substring(5).equals("moment"); | |
// Last parameter is not inclusive, but must be in bounds (≤ s.length()) | |
assert s.substring(2, 4).equals("uh"); | |
assert s.charAt(0) == 'b'; | |
// Can't use foreach loop | |
for (int i = 0; i < s.length(); i++) { | |
System.out.print(s.charAt(i)); | |
} | |
System.out.println(); | |
} | |
{ | |
String[] arr = {"This is a string", "bruh", "idk", "more", "words"}; | |
int sum = Arrays.stream(arr) | |
.filter(str -> str.length() <= 5) | |
.mapToInt(str -> str.length() * 2) | |
.sum(); | |
assert sum == 2 * (4 + 3 + 4 + 5); | |
} | |
{ | |
List<Boolean> b = new ArrayList<>(); | |
b.add(true); | |
b.add(true); | |
b.add(false); | |
b.add(true); | |
b.add(false); | |
List<String> a = b.stream() | |
.map(bool -> bool ? "yes" : "no") | |
.collect(Collectors.toList()); | |
// Mutable | |
a.add("end"); | |
for (String s : a) { | |
System.out.println(s); | |
} | |
// Or | |
b.stream() | |
.map(bool -> bool ? "yes" : "no") | |
.forEach(System.out::println); | |
} | |
{ | |
var words = Arrays.asList("A", "B", "C", "D").stream(); | |
words.collect(Collectors.joining()); //ABCD | |
words.collect(Collectors.joining(",")); //A,B,C,D | |
words.collect(Collectors.joining(",", "{", "}")); //{A,B,C,D} | |
} | |
{ | |
List<Integer> in = new ArrayList<>(); | |
// Arrays.stream returns an IntStream if it is one, otherwise use mapToInt | |
IntSummaryStatistics stat = in.stream().mapToInt(x -> x).summaryStatistics(); | |
stat.getAverage(); | |
stat.getCount(); | |
stat.getMax(); | |
stat.getMin(); | |
stat.getSum(); | |
in.stream().mapToInt(x -> x).average().getAsDouble(); | |
in.stream().mapToInt(x -> x).count(); | |
in.stream().mapToInt(x -> x).max(); | |
in.stream().mapToInt(x -> x).min(); | |
in.stream().mapToInt(x -> x).sum(); | |
} | |
} | |
class InterfaceDemo implements Comparable<InterfaceDemo> { | |
public int compareTo(InterfaceDemo interfaceDemo) { | |
// -1: Less than | |
// 0: Equal to | |
// 1: Greater than | |
return 0; | |
} | |
public boolean equals(Object obj) { | |
return obj instanceof InterfaceDemo; | |
} | |
public String toString() { | |
return "Interface Demo"; | |
} | |
} | |
enum EnumDemo { | |
Option1, | |
Option2, | |
SpecificValue; | |
EnumDemo() { | |
// Do whatever here | |
} | |
// just a regular class | |
String getInfo() { | |
switch (this) { | |
// Don't need to say EnumDemo.Option1 | |
case Option1: | |
return "Option 1"; | |
case Option2: | |
return "Option 2"; | |
default: | |
return "Specific Value"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment