Learn Java Stream from SQL Query : SELECT NAME FROM STUDENT

Maret 08, 2018

Hi, Programmer. Now I will share about feature in java called Stream. Stream is available from java 8. it is very usefull. We can cut progress time from looping something and it can store data like Querying in SQL like Select name from employee to get only name from employee object. Okay I will Introduce Java Stream.


Remember to use Java 8 or higher

The main requirement to use stream is you should installed java 8.

Where is Stream come from?

Java Stream is new method from Object List. so when you have object of list and put " .stream()" will available.

List<Object> list = new ArrayList<>();
list.stream();

First I create Student Object

2. Try using Stream

here I try to use 
select name from student
and here how it works


And here the output :


Rico
Arisandy
Wijaya
Soekarno
Diponegoro
Patimura
Fatmawati
Hatta

Explanation :

I will explain as easy like you use SQL Query. 

First : Use .stream() = FROM LIST

Stream is must coded if you want to use it. in here we have object first so we begin with FROM.

Second : SELECT  = .map()

after stream there is .map() method. We can call it begin of SELECT in SQL. Parameter in method you could use method from the class which inside parameter. 

Third : (*  / name / something) = Object::getter()

inside map parameter, we put Student object, and inside Student there is getName() method from setter getter. So we will get return from getName() method for every object inside list we used stream. 

Final : .collect(Collectors.toList());

After that if we want to use the result, we can put the result inside new List object.

Next we will learn filter method or we can call WHERE in SQL Query.

You Might Also Like

2 komentar

  1. Nice ! In .net framework it's called linq, but based on some literation the performance is slower than foreach

    BalasHapus
    Balasan
    1. every language is compete to create something for face Big Data era. "Data is new Oil" right?

      Hapus

Like us on Facebook