I have a list of columns, I need to know if this list contains both columns that are keys as columns that are not keys.
I guarantee the existence of at least one column in the list
My column class:
public class Coluna {
public boolean chave;
public boolean isChave() { return chave; }
}
My verification is now:
List<Coluna> minhasColunas = ...;
Predicate<Coluna> ehChave = Coluna::isChave;
boolean possuiColunasMistas =
minhasColunas.stream().anyMatch(ehChave) &&
minhasColunas.stream().anyMatch(ehChave.negate());
Is there any more idiomatic way of doing this check? I found it very strange to go through two streams to get the result.
On a more imperative check, I would do the following:
List<Coluna> minhasColunas = ...;
boolean achouChave = false;
boolean achouNaoChave = false;
for (Coluna c: minhasColunas) {
if (c.chave) {
achouChave = true;
if (achouNaoChave) {
break;
}
} else {
achouNaoChave = true;
if (achouChave) {
break;
}
}
}
boolean possuiColunasMistas = achouChave && achouNaoChave;
After creating the question, and chatting with @Anderson Carlos Woss , I ended up creating some test scenarios for that question. They are available here: link
So if you want to validate your own answer, implement the boolean possuiColunasMistas(List<Coluna> colunas)
method and then just run the test cases with JUnit.