Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20231104-KursPodstawowyALX
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
20231104-KursPodstawowyALX
Commits
3a0d6724
Commit
3a0d6724
authored
Jan 07, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
filter i map
parent
6fcc26f7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
0 deletions
+52
-0
F1_WypiszWybranePola.java
src/main/java/emps/v3_funkcyjnie/F1_WypiszWybranePola.java
+13
-0
F2_WypiszBogatych.java
src/main/java/emps/v3_funkcyjnie/F2_WypiszBogatych.java
+39
-0
No files found.
src/main/java/emps/v3_funkcyjnie/F1_WypiszWybranePola.java
0 → 100644
View file @
3a0d6724
package
emps
.
v3_funkcyjnie
;
import
java.util.List
;
public
class
F1_WypiszWybranePola
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytajDane
();
emps
.
forEach
(
emp
->
System
.
out
.
printf
(
"Pracownik nr %d, %s %s (%s) zarabia %d\n"
,
emp
.
getEmployeeId
(),
emp
.
getFirstName
(),
emp
.
getLastName
(),
emp
.
getJobTitle
(),
emp
.
getSalary
()));
}
}
src/main/java/emps/v3_funkcyjnie/F2_WypiszBogatych.java
0 → 100644
View file @
3a0d6724
package
emps
.
v3_funkcyjnie
;
import
java.util.List
;
public
class
F2_WypiszBogatych
{
public
static
void
main
(
String
[]
args
)
{
List
<
Employee
>
emps
=
ObslugaCSV
.
wczytajDane
();
System
.
out
.
println
(
"BOGACI:"
);
// Z kolekcji pobieramy "strumień" i na tym strumieniu wykonujemy operację filtrowania,
// a następnie operację forEach → dla tych rekordów, które spełniły warunek filtrowania, zostanie wykonana operacja printf
emps
.
stream
()
.
filter
(
emp
->
emp
.
getSalary
()
>=
10_000
)
.
forEach
(
emp
->
System
.
out
.
printf
(
"Pracownik nr %d, %s %s (%s) zarabia %d\n"
,
emp
.
getEmployeeId
(),
emp
.
getFirstName
(),
emp
.
getLastName
(),
emp
.
getJobTitle
(),
emp
.
getSalary
()));
// Po wykonaniu filter / forEach lista nadal zawiera wszystkie rekordy.
// Operacje strumieniowe służą tylko do odczytuj danych. Nie modyfikują źródła danych - lista pozostaje bez zmian.
// Ogólnie taka konstrukcja w kodzie nazywa się "pipeline":
// - źródło_danych
// - operacje pośrednie (intermediate operation, może być wiele kroków), np: filter, map, limit, sorted, distinct, ...
// - operacja końcowa (terminal operation), np: forEach, forEachOrdered, reduce, collect, findFirst, ...
System
.
out
.
println
(
"\nBIEDNI:"
);
emps
.
stream
()
.
filter
(
emp
->
emp
.
getSalary
()
<
5000
)
.
map
(
emp
->
String
.
format
(
"%s %s (%s) zarabia %d"
,
emp
.
getFirstName
(),
emp
.
getLastName
(),
emp
.
getJobTitle
(),
emp
.
getSalary
()))
// tutaj jest już strumień Stringów
.
map
(
String:
:
toUpperCase
)
.
forEach
(
System
.
out
::
println
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment