-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.tex
161 lines (127 loc) · 4.25 KB
/
utilities.tex
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
\section{Hilfsfunktionen}
\begin{frame}[fragile]
\frametitle{Hilfsfunktionen für Listen}
\begin{block}{java.util.Collections.emptyList()}
\begin{lstlisting}
List<String> l = Collections.emptyList();
\end{lstlisting}
\end{block}
\pause
\begin{block}{com.google.common.collect.Lists.newArrayList(T...)}
\begin{lstlisting}
List<String> l = Lists.newArrayList("a", "b", "c", "d");
\end{lstlisting}
\end{block}
\pause
\begin{block}{java.util.AbstractList.subList(int, int)}
from-index inklusiv, to-index exklusiv
\begin{lstlisting}
List<String> l = ...
List<String> sub = l.subList(2,5);
\end{lstlisting}
\end{block}
\pause
\begin{block}{java.util.Collections.unmodifiableList(List\textless? extends T\textgreater)}
\begin{lstlisting}
Collections.unmodifiableList(list).add("a"); // wirft UnsupportedOperationException
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Hilfsfunktionen für Listen}
\begin{block}{java.util.Collections.singletonList(T)}
\begin{lstlisting}
List<String> list = Collections.singletonList("a");
\end{lstlisting}
\end{block}
\pause
\begin{block}{java.util.Collections.synchronizedList(List\textless T\textgreater)}
erzeugt eine synchronisierte List
\begin{lstlisting}
List<String> l = ...
List<String> s = Collections.synchronizedList(l);
\end{lstlisting}
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%
% Map %
%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Hilfsfunktionen für Maps}
\begin{block}{java.util.Collections.synchronizedMap(Map\textless K, V\textgreater)}
erzeugt eine synchronisierte Map
\begin{lstlisting}
Map<String, String> m = ...
Map<String, String> s = Collections.synchronizedMap(m);
\end{lstlisting}
\end{block}
\pause
\begin{block}{java.util.Collections.unmodifiableMap(Map\textless K, V\textgreater)}
\begin{lstlisting}
Map<String, String> unmodifiableMap = Collections.unmodifiableMap(map);
unmodifiableMap.put("a", "b"); // UnsupportedOperationException
\end{lstlisting}
\end{block}
\pause
\begin{block}{java.util.Collections.emptyMap()}
unmodizierbare, leere Map
\begin{lstlisting}
Map<String, String> empty = Collections.emptyMap();
empty.put("a", "b");// UnsupportedOperationException
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Hilfsfunktionen für Maps}
\begin{block}{java.util.Collections.singletonMap(K, V)}
unmodizierbare, ein-elementige Map
\begin{lstlisting}
Map<String, String> single = Collections.singletonMap("a", "b");
single.put("a", "b");// UnsupportedOperationException
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Hilfsfunktionen für Maps}
\begin{block}{java.util.Map.computeIfAbsent(K, Function\textless ? super K, ? extends V\textgreater)}
füge in eine existierende Liste ein, ggf. erzeuge diese Liste
\begin{lstlisting}
void addOld(Map<String, List<String>> map, String key, String value) {
List<String> list = map.get(key);
if (list == null) {
list = new ArrayList<>();
map.put(key, list);
}
list.add(value);
}
\end{lstlisting}
\pause
\begin{lstlisting}
void addNew(Map<String, List<String>> map, String key, String value) {
map.computeIfAbsent(key, x -> new ArrayList<>()).add(value);
}
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Hilfsfunktionen für Maps}
\begin{block}{java.util.Map.getOrDefault(Object, V)}
default value in Map
\begin{lstlisting}
// alt
Map<String, String> map = new HashMap<>();
String s = map.get("a");
if (s == null) {
s = "default";
}
System.out.println(s);
\end{lstlisting}
\pause
\begin{lstlisting}
Map<String, String> map = new HashMap<>();
map.put("a", "a");
System.out.println(map.getOrDefault("a", "default"));
System.out.println(map.getOrDefault("b", "default"));
\end{lstlisting}
\end{block}
\end{frame}