I can understand lambdas
expressions perfectly in simple cases like:
() -> 42 // Não recebe nada e sempre retorna "42"
x -> x*x // Recebe algo e retorna seu quadrado
(x,y) -> x + y // Recebe dois valores e retorna sua soma
But never is like this in the codes, but in a more complex, illegible way for me. The code snippet below searches for Elastic Search and returns it. After a refactor, it is necessary that list is BasicDBObject
but what does the next line do? I know only by debug the elements that are accessed and returned. What does "this code" mean? I say, not only this in specific but the "syntaxes" of streams?
BasicDBList list = esClient.search("oknok", "base_gerenciada", "{\"size\":9999999,\"query\":{\"bool\":{\"must\":[{\"match\":{\"last\":true}},{\"match\":{\"spec_virtual\":\"não\"}},{\"query_string\":{\"query\":\"" + search + "* OR spec_veiculo:" + search + "*\",\"default_operator\":\"AND\"}}]}}}");
return list.parallelStream().map((temp) -> (BasicDBObject) ((BasicDBObject) temp).get("_source")).collect(Collectors.toList());
Debug
**ChangingthereturntoBasicDBObject
,Iwasabletorewritethissection:
BasicDBObjectdbObject=esClient.search("oknok", "base_gerenciada", "{\"size\":9999999,\"query\":{\"bool\":{\"must\":[{\"match\":{\"last\":true}},{\"match\":{\"spec_virtual\":\"não\"}},{\"query_string\":{\"query\":\"" + search + "* OR spec_veiculo:" + search + "*\",\"default_operator\":\"AND\"}}]}}}");
BasicDBList list = (BasicDBList) ((BasicDBObject) dbObject.get("hits")).get("hits");
for(int i = 0; i < list.size(); i++){
myList.add((BasicDBObject) ((BasicDBObject) list.get(i)).get("_source"));
}
return myList;