Similarly to Go, I would normally avoid putting a cheat sheet for general language things. However, because I don't use Java often, I'd like to make note of how to do things in Java I often do in C#, etc.
for (int item : collection) { }
TODO: Research backend for this syntax.
Does item
have to be a Collection
? Implement an interface? Just have `MoveNext method?
The drop-in LINQ for Java are Java Streams.
Arrays.stream(arr)
collection.stream()
->
From what I have found, there is no drop-in C# Func
in Java. There is Function<T, R>
and Supplier<T>
for a function that takes no arguments.
If you want to reference a method to, for example, pass to a Stream function, you can reference it with the ::
operator.
For example
List.of("a", "b").stream().forEach(System.out::println);
When parsing numbers, use static methods in their boxed types:
In general
Number.parseNumber("4")
For example
Double.parseDouble("0.01")
C# nameof
drop-in uses reflection in Java. If you just need the class name, a one-liner:
ClassNameFoo.class.getSimpleName()