How to use Bilge and stream?

Asked

Viewed 357 times

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 inserir a descrição da imagem aqui

**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?

  • 1

    Java 8, I believe :)

  • 1

    Yes, this is Java 8. I’m glad I’m not the only one who’s not familiar yet!

  • this java8 is getting complicated :P

1 answer

6


Follow the explanatory view of the syntax with detailed references in links:

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

To new Collection interface (java 8) brings the default implementation of both:

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

In your case being called explicitly by your Collection:

It should be noted that the parallelStream, API itself decomposes the treatment of stream parallelizing its execution transparently.

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

the map method -implemented by stream-returns a stream element by applying the function informed, in your case:

(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 element temp
  • Followed by a get on Linkedhashmap by String key


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


And finally Collect, which applies a Mutable Reduction, aggregating the elements returned by the function in the container specified, in your case a Collectors.toList() -implementation of List- or could also be a Stringbuilder for example using Collectors.Oining(", ").

.collect(Collectors.toList())


Overview of Stream Flow: inserir a descrição da imagem aqui

Oracle Blog Link with more details.

  • 1

    Very good answer. Streams seems to me you need a good practice, after a few days reading about and then rereading your answer is ideal

Browser other questions tagged

You are not signed in. Login or sign up in order to post.