15
I can understand expressions lambdas
perfectly in simple cases such as:
() -> 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 thus in the codes and yes in a more complex way, unreadable for me. The code snippet below Elastic Search and returns it. After a refactor, it is necessary to list BasicDBObject
but what does the next line do? I only know by debug the elements that are accessed and returned. What does "mean" this code? I mean, not only this specific but the "syntax" 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
**Changing the return to BasicDBObject
, I managed to rewrite this excerpt:
BasicDBObject dbObject = 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;
This code is really java?
– RSinohara
Java 8, I believe :)
– Maicon Carraro
Yes, this is Java 8. I’m glad I’m not the only one who’s not familiar yet!
– Daniela Morais
this java8 is getting complicated :P
– João Reis