How to use lambdas and stream?

15

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;
    
asked by anonymous 22.06.2015 / 21:10

1 answer

6

Here is the explanatory overview of the syntax with detailed references on links:

BasicDBList list = (... Implementa Collection ...)

New Collection (java 8) brings < a href="http://zeroturnaround.com/rebellabs/java-8-explained-default-methods/"> default implementation of both:

default Stream<E> stream() {
default Stream<E> parallelStream() {

In your case being explicitly called by your Collection:

  

Note that parallelStream was used, in that the API itself decomposes the   stream by streamlining its execution transparently.

list.parallelStream()
                      .map(...sua funcao...)  
  

o method map -implemented by stream- returns an element of the stream by applying to function informed, if any:

(temp) -> (BasicDBObject) ((BasicDBObject) temp).get("_source")


temp is the element of the stream to be applied to the function that is:

  • A cast in the temp element
  • Followed by a get on LinkedHashMap by the String key


((BasicDBObject) temp) // cast
                      .get("_source") // obtendo Map<String, BasicDBObject>


And finally collect, which applies a Mutable Reduction , adding the elements returned by the specified container , in your case a Collectors.toList () > -implementation of List - or it could also be a StringBuilder for example using Collectors.joining (",") .

.collect(Collectors.toList())


Stream Stream Overview:

Oracle Blog Link in more detail.

    
24.06.2015 / 08:46