Tips and Traps¶
Prefer
Dataclass
tonamedtuple
for many reasons.A namedtuple is immutable while a dataclass can be both mutable (
frozen=False
which is the default) or immutable (frozen=True
).However, namedtuple does have one advantage over dataclass. Members of a namedtuple is assible both via the dot operator and index. In situations where both dot accessing and index accessing of members is required, a namedtuple comes handy. For examples, a list of namedtuple objects can be used as the data for creating a pandas DataFrame but not a list of dataclass objects.
Collections in Java
Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
Oracel Java Tutorial on Collections
ArrayList
-
You can point an element of
Array
orArrayList
tonull
, but remember that anull
reference cannot invoke any method. For example, if you …
Collections in Kotlin
Fold vs Reduce¶
fold
takes an initial value,
and the first invocation of the lambda you pass to it
will receive that initial value
and the first element of the collection as parameters.
reduce
doesn't take an initial value,
but instead starts with the first element of the collection as the accumulator (called sum in the following example).
MutableList in Kotlin
MutableList vs ArrayList¶
MutableList is ArrayList in Kotlin currently.
Create an Empty MutableList¶
Below is the most idiomatical way to create an empty mutable list in Kotlin.
OOP in Kotlin
Hashmap in Kotlin
References¶
- LinkedHashMap preserves the insertion order (which is similar to dict in Python 3.7+).