Skip to content

Instantly share code, notes, and snippets.

@ayago
Created July 26, 2019 03:21
Show Gist options
  • Save ayago/1cd28f4dd44f25da3904808cd324462c to your computer and use it in GitHub Desktop.
Save ayago/1cd28f4dd44f25da3904808cd324462c to your computer and use it in GitHub Desktop.
Tuples in Java
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.math.BigDecimal;
import java.util.function.Function;
import static java.lang.String.format;
class SampleTuples {
public static void main(String[] args) {
Tuple<String, BigDecimal> yourSalary = new Tuple<>("Lead", new BigDecimal("300000"));
Tuple3<String, BigDecimal, Function<String, String>> func = new Tuple3<>(
"Lead", new BigDecimal("300000"), s -> format("Libre naman %s", s));
}
/**
* Compound data
*
* @param <A> First element type
* @param <B> Second element type
*/
@AllArgsConstructor
@Getter
public static class Tuple<A, B> {
private final A first;
private final B second;
}
/**
* Compound data
*
* @param <A> First element type
* @param <B> Second element type
*/
@AllArgsConstructor
@Getter
public static class Tuple3<A, B, C> {
private final A first;
private final B second;
private final C third;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment