Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Various ways to check is a 2-dimensional ArrayList contains a specified value
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;
public class Contains {
//------------------------------------------------------------------------------
public static boolean contains4(List< ArrayList<String> > matrix, Object obj) {
// 1. Get a stram of lists
// 2. Flatten them into a single map
// 3. Apply condition that at least one match
return matrix.stream()
.flatMap(List::stream)
.anyMatch(o ->
Objects.equals(o, obj)
);
}
//------------------------------------------------------------------------------
public static boolean contains3(List< ArrayList<String> > matrix, Object obj) {
// 1. Get a stream of lists
// 2. Flatten them into a single map
// 3. Convert to a list
// 4. Perform liean search on the list
return matrix.stream()
.flatMap(List::stream)
.collect(Collectors.toList())
.contains(obj);
}
//------------------------------------------------------------------------------
public static boolean contains2(List< ArrayList<String> > matrix, Object obj) {
// Iterate over all rows
for (List<?> row : matrix) {
// Iterate over each value per row
for (Object o : row) {
// Compare
if (Objects.equals(o, obj)) {
return true;
}
}
}
return false;
}
//------------------------------------------------------------------------------
public static boolean contains1(List< ArrayList<String> > matrix, Object obj) {
// 1. Check if the matrix is empty
if (matrix.isEmpty()) {
return false;
}
// 2. Get matrix dimensions
int rows = matrix.size();
int cols = matrix.get(0).size();
Object temp;
// Loop from 0 to row count
for (int i = 0; i < rows; i++) {
// Loop from 0 to column count
for (int j = 0; j < cols; j++) {
// Get matrix[i][j]
temp = matrix.get(i).get(j);
// Compare
if (Objects.equals(obj, temp)) {
return true;
}
}
}
return false;
}
//------------------------------------------------------------------------------
public static void main(String[] args) {
var row = new ArrayList<String>();
row.add("A");
row.add("B");
row.add("C");
var matrix = new ArrayList< ArrayList<String> >();
matrix.add(row);
matrix.add(row);
matrix.add(row);
test(matrix, "A");
for (List<?> r : matrix) {
r.remove("A");
}
test(matrix, "A");
}
//------------------------------------------------------------------------------
public static void display(List< ArrayList<String> > matrix) {
for (List<String> r : matrix) {
System.out.println(r);
}
}
//------------------------------------------------------------------------------
public static void test(List< ArrayList<String> > matrix, Object obj) {
display(matrix);
System.out.format(
"--------------------\n" +
"contains1(%s) = %s\n" +
"contains2(%s) = %s\n" +
"contains2(%s) = %s\n" +
"contains2(%s) = %s\n" +
"--------------------\n\n",
obj, contains1(matrix, obj),
obj, contains2(matrix, obj),
obj, contains3(matrix, obj),
obj, contains4(matrix, obj)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.