Skip to content

Instantly share code, notes, and snippets.

@thiagolocatelli
Created May 12, 2019 18:45
Show Gist options
  • Save thiagolocatelli/fd9708c64c49e558ff2916452f5946e7 to your computer and use it in GitHub Desktop.
Save thiagolocatelli/fd9708c64c49e558ff2916452f5946e7 to your computer and use it in GitHub Desktop.
Value added to the matrix: 0_0
Value added to the matrix: 0_1
Value added to the matrix: 0_2
Value added to the matrix: 1_0
Value added to the matrix: 1_1
Value added to the matrix: 1_2
Value added to the matrix: 2_0
Value added to the matrix: 2_1
Value added to the matrix: 2_2
Found: 1_2
import java.util.Comparator;
import java.util.Objects;
import java.util.function.Consumer;
public class TestGenerics {
public static class A<T> {
private T var;
public A(T elm) {
var = elm;
}
public void fun(T[][] foo, Comparator<T> comparator, Consumer<T> invoke) {
for(int r=0 ; r < foo.length; r++) {
for (int c = 0; c < foo[r].length; c++) {
if(Objects.compare(foo[r][c], var, comparator) == 0) {
invoke.accept(foo[r][c]);
}
}
}
}
}
public static void main(String args[]) {
A<String> a = new A<String>("1_2");
String[][] matrix = new String[3][3];
for(int r=0 ; r < matrix.length; r++) {
for (int c = 0; c < matrix[r].length; c++) {
matrix[r][c] = r + "_" + c;
System.out.println("Value added to the matrix: " + matrix[r][c]);
}
}
a.fun(matrix, String::compareTo, found -> {
// Here you execute anything with "var"
System.out.println("Found: " + found);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment