Skip to content

Commit 2299a03

Browse files
committed
Add some missing elements
1 parent 96cb9ce commit 2299a03

1 file changed

Lines changed: 85 additions & 47 deletions

File tree

doc/CheatSheet-Java.md

Lines changed: 85 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
String-Formatierung, Konvertierungen
2-
- Erstellen von Strings mit `String.format(String format, Object... argumente)`
2+
- Erstellen von Strings mit `String.format(String format, Object... arguments)`
33
- `%n` ist Zeilenumbruch
44
- `%s` Platzhalter für String
55
- `%d` Platzhalter für Ganzzahl, `%03d` mit drei Stellen und führender Null
6-
- `%f` Platzhalter für Kommazahl, `%.3f` mit 3 Nachkommastellen
7-
- String zu Ganzzahl: `int number = Integer.parseInt(String number)`
6+
- `%f` Platzhalter für Fließkommazahl, `%.3f` mit 3 Nachkommastellen
7+
- String zu Ganzzahl: `int number = Integer.parseInt(String number)` (kann Exception werfen)
88
- Ganzzahl zu String: `String text = String.valueOf(int number)`
9-
- String zu Kommazahl: `double number = Double.parseDouble(String number)`
10-
- Kommazahl zu String: `String text = String.valueOf(double number)`
11-
- Direkte Ausgabe mit `System.out.printf(String format, Object... argumente)`
9+
- String zu Fließkommazahl: `double number = Double.parseDouble(String number)` (kann Exception werfen)
10+
- Fließkommazahl zu String: `String text = String.valueOf(double number)`
11+
- Direkte Ausgabe mit `System.out.printf(String format, Object... arguments)`
1212

13-
Methoden der Klasse Math
14-
- `Math.max(a, b)`, `Math.min(a, b)`, `Math.abs(a)` - Maximum, Minimum, Absolutwert
15-
- `Math.round(a)` rundet `double` nach `long`, bzw. `float` nach `int` (kaufmännisch)
16-
- `Math.pow(a, b)` berechnet a hoch b
17-
- `Math.sqrt(a)` berechnet die Wurzel
13+
Statische Methoden der Klasse Math
14+
- `Math.max(a, b)`, `Math.min(a, b)`, `Math.abs(a)`: Maximum, Minimum, Absolutwert
15+
- `Math.round(a)`: rundet `double` nach `long`, bzw. `float` nach `int` (kaufmännisch)
16+
- `Math.pow(a, b)`: berechnet a hoch b
17+
- `Math.sqrt(a)`: berechnet die Wurzel
1818

1919
Listen vs. Arrays
2020

@@ -34,60 +34,82 @@ Listen vs. Arrays
3434
| Leeren | nicht möglich | `list.clear()` |
3535
| Suchen | `ArrayUtils.indexOf(array, element)` | `list.indexOf(element)` |
3636
| | `ArrayUtils.contains(array, element)` | `list.contains(element)` |
37-
| Ausgabe | `Arrays.toString(array)` | `list.toString() ` |
38-
| Vergleich | `Arrays.equals(array, other)` | `list.equals(other) ` |
39-
40-
Methoden der Klasse Set
41-
42-
- Erzeugen: `List<Type> set = new HashSet<Type>()`
43-
- Hinzufügen: `set.add(element)`
44-
- Entfernen: `Type element = set.remove(element)`
45-
- Suchen: `set.contains(element)`
46-
- Leeren: `set.clear()`
47-
48-
Methoden der Klasse Collections
49-
50-
- Sortieren: `Collections.sort(...)`
51-
- Reihenfolge umdrehen: `Collections.reverse(...)`
52-
- Mit Wert füllen: `Collections.fill(...)`
53-
54-
Methoden der Klasse String
37+
| Ausgabe | `Arrays.toString(array)` | `list.toString()` |
38+
| Vergleich | `Arrays.equals(array, other)` | `list.equals(other)` |
39+
40+
Listen vs. Sets
41+
42+
| Operation | Set (HashSet oder TreeSet) | List (ArrayList oder LinkedList) |
43+
|---------------|-----------------------------|----------------------------------|
44+
| Definition | `Set<Type> set` | `List<Type> list` |
45+
| Erstellen | `set = new HashSet<Type>()` | `list = new ArrayList<Type>()` |
46+
| Lesen | nicht möglich | `Type element = list.get(index)` |
47+
| Größe | `set.size()` | `list.size()` |
48+
| Überschreiben | nicht möglich | `list.set(index, element)` |
49+
| Hinzufügen | `set.add(element)` | `list.add(element)` |
50+
| | `set.addAll(elements)` | `list.addAll(elements)` |
51+
| Einfügen | nicht möglich | `list.add(index, element)` |
52+
| Entfernen | `set.remove(element)` | `list.remove(element)` |
53+
| | `set.removeAll(elements)` | `list.removeAll(elements)` |
54+
| | | `list.remove(index)` |
55+
| Leeren | `set.clear()` | `list.clear()` |
56+
| Suchen | `set.contains(element)` | `list.contains(element)` |
57+
| | | `list.indexOf(element)` |
58+
| Ausgabe | `set.toString() ` | `list.toString() ` |
59+
| Iterator | `set.iterator() ` | `list.iterator() ` |
60+
61+
Statische Methoden der Klasse Collections
62+
63+
- Sortieren:
64+
- `Collections.sort(List<Type> list)` (`Type` muss `Comparable` sein)
65+
- `Collections.sort(List<Type> list, Comparator<Type> c)`
66+
- Reihenfolge umdrehen: `Collections.reverse(List<Type> list)`
67+
- Mit Wert füllen: `Collections.fill(List<Type> list, Type value)`
68+
- Wert suchen: `int Collections.binarySearch(List<Type> list, Type key)` (`Type` muss `Comparable` sein)
69+
70+
Methoden rund um Strings
5571

5672
- Suchen:
5773
- `boolean contains(String pattern)`
74+
- `boolean startsWith(String pattern)`
75+
- `boolean endsWith(String pattern)`
5876
- `int indexOf(String pattern)`
59-
- `int indexOf(String pattern, int from)`
6077
- Größe: `length()`, `isEmpty()`, `isBlank()`
61-
- Ersetzen: `String replace(String muster, String ersatz)`
62-
- Teilbereiche:
63-
- `String substring(int from)`
64-
- `String substring(int from, int to)`
65-
- `String strip()`, `String toUppercase()`, `String toLowercase()`
66-
- String in Zeichen umwandeln: `char[] toCharArray()`
78+
- Ersetzen: `String replace(String pattern, String replacement)`
79+
- Teilbereich:
80+
- `String substring(int from, int to)` (Index)
81+
- `String StringUtils.substringBetween(String str, String open, String close)` (Text)
82+
- Zusammenfügen: `String join(String delimiter, String... texts)`
83+
- `String strip()`, `String toUpperCase()`, `String toLowerCase()`
84+
- String in Zeichenarray umwandeln: `char[] toCharArray()`
6785
- Vergleichen (lexikografisch): `int compareTo(String other)`
86+
- Suchen: `boolean StringUtils.containsAnyIgnoreCase(String value, String... patterns)`
87+
- Splitten: `String[] StringUtils.split(String separator)`
6888

69-
Methoden der Klasse Character
89+
Statische Methoden der Klasse Character
7090
- `boolean Character.isLetter(char c)`
7191
- `boolean Character.isDigit(char c)`
7292
- `boolean Character.isWhitespace(char c)`
7393
- `boolean Character.isUpperCase(char c)`
7494
- `boolean Character.isLowerCase(char c)`
75-
- `for (char zeichen : text.toCharArray()) { ... }`
95+
- `for (char ch : text.toCharArray()) { ... }`
7696

7797
Testen
7898
- Markierung einer Testmethode: `@Test`
7999
- Assertions
80-
- `assertThat(Type expected).isEqualTo(Type actual)` (equals überschrieben)
81-
- `assertThat(Type expected).usingRecursiveComparison().isEqualTo(Type actual)` (sonst)
82-
- `assertThat(boolean expected).isTrue()` oder `assertThat(boolean expected).isFalse()`
83-
- `assertThat(String expected).contains("o").startsWith("Hello").endsWith("World")`
84-
- `assertThat(Collection<Type> expected).contains(Type actual1, Type actual2, ...)`
85-
- `assertThat(Collection<Type> expected).containsExactly(Type actual1, Type actual2, ...)`
86-
- `assertThat(Collection<Type> expected).first().isEqualTo(Type actual)`
87-
- `assertThatExceptionOfType(Type.class).isThrownBy(() -> [CODE]).withMessageContaining("Error");`
100+
- `assertThat(Type actual).isSame(Type expected)`
101+
- `assertThat(Type actual).isEqualTo(Type expected)` (equals überschrieben)
102+
- `assertThat(Type actual).usingRecursiveComparison().isEqualTo(Type expected)` (sonst)
103+
- `assertThat(boolean actual).isTrue()` oder `assertThat(boolean expected).isFalse()`
104+
- `assertThat(String actual).contains("o").startsWith("Hello").endsWith("World")`
105+
- `assertThat(Collection<Type> actual).contains(Type expected1, Type expected2, ...)`
106+
- `assertThat(Collection<Type> actual).containsExactly(Type expected1, Type expected2, ...)`
107+
- `assertThat(Collection<Type> actual).first().isEqualTo(Type expected)`
108+
- `assertThatExceptionOfType(Type.class).isThrownBy(() -> code).withMessageContaining("Error");`
88109

89110
Exceptions
90111
- Werfen mit `throw new Type("Meldungstext mit Kontext")`
112+
- Testen mit: `assertThatExceptionOfType(Type.class).isThrownBy(() -> [CODE]).withMessageContaining("Error");`
91113
- Fehlerbehandlung mit
92114
```
93115
try {
@@ -117,5 +139,21 @@ Vererbung und Interfaces
117139
Methoden der Klasse Object
118140
- Vergleich: `public boolean equals(Object obj)`
119141
- HashCode: `public int hashCode()`
120-
- Beschreibung
142+
- Beschreibung: `public String toString()`
143+
144+
Wichtige Schnittstellen
145+
146+
- Comparable: `public int compareTo(Type other)`
147+
- Comparator: `public int compare(Type o1, Type o2)`
148+
- Iterable: `public Iterator<Type> iterator()`
149+
- Iterator: `public boolean hasNext()`, `public Type next()`
150+
151+
Maps
152+
- Definition: `Map<KeyType, ValueType> map`
153+
- Erstellen: `map = new HashMap<KeyType, ValueType>()`
154+
- Hinzufügen: `map.put(KeyType key, ValueType value)`
155+
- Lesen: `ValueType value = map.get(KeyType key)`
156+
- Entfernen: `ValueType value = map.remove(KeyType key)`
157+
- Größe: `map.size()`
158+
- Leeren: `map.clear()`
121159

0 commit comments

Comments
 (0)