Skip to content

Latest commit

 

History

History
74 lines (45 loc) · 1.32 KB

java.md

File metadata and controls

74 lines (45 loc) · 1.32 KB

Java

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.

C# foreach

for (int item : collection) { }

TODO: Research backend for this syntax. Does item have to be a Collection? Implement an interface? Just have `MoveNext method?

LINQ

The drop-in LINQ for Java are Java Streams.

Create

Arrays

Arrays.stream(arr)

Collections

collection.stream()

Functions as Data

Lambda syntax

->

C# Func

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.

Method Reference Operator

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);

Parsing

When parsing numbers, use static methods in their boxed types:

In general

Number.parseNumber("4")

For example

Double.parseDouble("0.01")

C# nameof

C# nameof drop-in uses reflection in Java. If you just need the class name, a one-liner:

ClassNameFoo.class.getSimpleName()