Since it is a personal memo, it may be added appropriately or left unattended.
Starting with JDK9, it's better to use List.of
than ʻArrays.asList. The
List returned by ʻArrays.asList
is just an array wrapper, fixed in size but capable of changing elements. On the other hand, the List
returned by List.of
cannot change its elements and becomes immutable.
This looks like a mysterious syntax, but that's what it is.
Just as new String (data)
can be written as String :: new
.
final Function<String, Price> CONVERT_STRING_TO_PRICE =
str -> str.substring(0, str.length() - 1).transfer(Price::of);
Define the field
return inputs.stream()
.map(CONVERT_STRING_TO_PRICE)
.collect(toList());
It may be used like this.
But this is a normal method definition,
Price stringToPrice(String str) {
return str.substring(0, str.length() - 1).transfer(Price::of);
}
It's better to use method references.
return inputs.stream()
.map(Converters::stringToPrice)
.collect(toList());
You can write comments in JavaDoc properly, and methods are easier to read than higher-order functions. Also, lambdas are eventually compiled into method definitions and method references, which is a bit of a waste.
Since it is not Serializable
, it is better to use a normal value with @Nullable
etc.
Unless it is a method that manipulates Stream itself, it is better not to return Stream.
As a standard, you can return Stream <T>
, but something that returns a concrete type such as Stream <String>
is not very good.
Or when you want to perform "stream processing" with I / O like Files.lines.
IntStream.range(0, chars.length)->mapToObject(i -> chars[i])
anyMatch
for (var a : list) {
if (condition(a)) {
return true;
}
}
return false
↓
return list.stream().anyMatch(this::condition);
allMatch
for (var a : list) {
if (!condition(a)) {
return false;
}
}
return true
↓
return list.stream().allMatch(this::condition);
noneMatch
for (var a : list) {
if (condition(a)) {
return false;
}
}
return true
↓
return list.stream().noneMatch(this::condition);
ʻOptional` is a special collection of elements 0 or 1, so you don't need to enclose the Stream or List further and you should be able to handle it with Stream.of () or emptyList ().
if
Then the normal system, else the abnormal system
if (cond) {
proc
} else {
error
}
However, when the abnormal system returns / throws in one line, put the abnormal system in if and escape early.
if (!cond) {
throw error
}
proc
!(aa && bb)
It is better to avoid inversion by enclosing it in large parentheses like ! (Aa && bb)
.
if (!(hasValue && isValid)) {
error
}
Than
if (!hasValue || !isValid) {
error
}
You don't have to cache the results in your brain. However, if ʻelse` is attached
if (!hasValue || !isValid) {
error
} else {
proc
}
Than
if (hasValue && isValid) {
proc
} else {
error
}
Does not have to be reversed. In this case, the policy of changing the normal system to then is also followed. Also, the IDE should do this kind of logic reversal, so it's better not to do it by hand.
enum
When it has parameters, add @AllArgsConstructor
and @Getter
.
@Value
cannot be used for ʻenum`.
@AllArgsConstructor
@Getter
enum Hoge {
A("a-desu"),
B("b-dayo");
private String message;
}
ʻInteger and
Long
valueOf` return a wrapper object.
So code like this is useless
int n = Integer.valueOf(str);
Use parseInt
if you want to use it as a primitive
int n = Integer.parseInt(str);
catch (IOException ex) {
throw new UncehckedIOException(ex);
}
@VisibleForTesting
String createId(String seed) {
}
Recommended Posts