Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
kurs_java_alx_20240321
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
kurs_java_alx_20240321
Commits
06752568
Commit
06752568
authored
May 09, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wczytywanie strumieniowe prosto z pliku
parent
ae2a34c3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
75 additions
and
4 deletions
+75
-4
Grupowanie99.java
src/emps/v3_funkcyjnie/Grupowanie99.java
+23
-0
ObslugaCSV.java
src/emps/v3_funkcyjnie/ObslugaCSV.java
+36
-4
S2_Filtrowanie.java
src/emps/v3_funkcyjnie/S2_Filtrowanie.java
+16
-0
No files found.
src/emps/v3_funkcyjnie/Grupowanie99.java
0 → 100644
View file @
06752568
package
emps
.
v3_funkcyjnie
;
import
java.util.IntSummaryStatistics
;
import
java.util.Map
;
import
java.util.TreeMap
;
import
java.util.stream.Collectors
;
public
class
Grupowanie99
{
public
static
void
main
(
String
[]
args
)
{
Map
<
String
,
IntSummaryStatistics
>
grupy
=
ObslugaCSV
.
wczytajStrumieniowo
(
"emps.csv"
)
.
collect
(
Collectors
.
groupingBy
(
Employee:
:
getJobTitle
,
TreeMap:
:
new
,
Collectors
.
summarizingInt
(
Employee:
:
getSalary
)));
grupy
.
forEach
((
job
,
stats
)
->
{
System
.
out
.
printf
(
"| %-32s | %2d | %5d | %8.2f | %5d |%n"
,
job
,
stats
.
getCount
(),
stats
.
getMin
(),
stats
.
getAverage
(),
stats
.
getMax
());
});
}
}
src/emps/v3_funkcyjnie/ObslugaCSV.java
View file @
06752568
...
@@ -2,11 +2,15 @@ package emps.v3_funkcyjnie;
...
@@ -2,11 +2,15 @@ package emps.v3_funkcyjnie;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.io.PrintWriter
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.time.LocalDate
;
import
java.time.LocalDate
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Scanner
;
import
java.util.Scanner
;
import
java.util.stream.Stream
;
public
class
ObslugaCSV
{
public
class
ObslugaCSV
{
public
static
List
<
Employee
>
wczytaj
()
{
public
static
List
<
Employee
>
wczytaj
()
{
...
@@ -24,10 +28,7 @@ public class ObslugaCSV {
...
@@ -24,10 +28,7 @@ public class ObslugaCSV {
scanner
.
nextLine
();
// pomijamy pierwszą linię
scanner
.
nextLine
();
// pomijamy pierwszą linię
while
(
scanner
.
hasNextLine
())
{
while
(
scanner
.
hasNextLine
())
{
String
linia
=
scanner
.
nextLine
();
String
linia
=
scanner
.
nextLine
();
String
[]
t
=
linia
.
split
(
";"
,
-
1
);
Employee
emp
=
parsujLinie
(
linia
);
Employee
emp
=
new
Employee
(
Integer
.
parseInt
(
t
[
0
]),
t
[
1
],
t
[
2
],
t
[
3
],
Integer
.
parseInt
(
t
[
4
]),
LocalDate
.
parse
(
t
[
5
]),
t
[
6
],
t
[
7
],
t
[
8
],
t
[
9
],
t
[
10
]);
emps
.
add
(
emp
);
emps
.
add
(
emp
);
}
}
}
catch
(
FileNotFoundException
e
)
{
}
catch
(
FileNotFoundException
e
)
{
...
@@ -37,6 +38,29 @@ public class ObslugaCSV {
...
@@ -37,6 +38,29 @@ public class ObslugaCSV {
return
emps
;
return
emps
;
}
}
public
static
Stream
<
Employee
>
wczytajStrumieniowo
(
Path
plik
)
{
try
{
return
Files
.
lines
(
plik
)
.
skip
(
1
)
.
map
(
ObslugaCSV:
:
parsujLinie
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
Stream
.
empty
();
}
}
public
static
Stream
<
Employee
>
wczytajStrumieniowo
(
File
plik
)
{
return
wczytajStrumieniowo
(
plik
.
toPath
());
}
public
static
Stream
<
Employee
>
wczytajStrumieniowo
(
String
sciezka
)
{
return
wczytajStrumieniowo
(
Path
.
of
(
sciezka
));
}
public
static
Stream
<
Employee
>
wczytajStrumieniowo
()
{
return
wczytajStrumieniowo
(
"emps.csv"
);
}
public
static
void
zapisz
(
List
<
Employee
>
lista
,
File
plik
)
{
public
static
void
zapisz
(
List
<
Employee
>
lista
,
File
plik
)
{
try
(
PrintWriter
out
=
new
PrintWriter
(
plik
))
{
try
(
PrintWriter
out
=
new
PrintWriter
(
plik
))
{
out
.
println
(
out
.
println
(
...
@@ -59,5 +83,13 @@ public class ObslugaCSV {
...
@@ -59,5 +83,13 @@ public class ObslugaCSV {
public
static
void
zapisz
(
List
<
Employee
>
lista
)
{
public
static
void
zapisz
(
List
<
Employee
>
lista
)
{
zapisz
(
lista
,
"emps.csv"
);
zapisz
(
lista
,
"emps.csv"
);
}
}
private
static
Employee
parsujLinie
(
String
linia
)
{
String
[]
t
=
linia
.
split
(
";"
,
-
1
);
Employee
emp
=
new
Employee
(
Integer
.
parseInt
(
t
[
0
]),
t
[
1
],
t
[
2
],
t
[
3
],
Integer
.
parseInt
(
t
[
4
]),
LocalDate
.
parse
(
t
[
5
]),
t
[
6
],
t
[
7
],
t
[
8
],
t
[
9
],
t
[
10
]);
return
emp
;
}
}
}
src/emps/v3_funkcyjnie/S2_Filtrowanie.java
0 → 100644
View file @
06752568
package
emps
.
v3_funkcyjnie
;
public
class
S2_Filtrowanie
{
// Program wypisuje tylko zarabiających >= 10 tys,
// realizujemy to filtrowaniem, ale przy okazji pokazuję Wam schemat "filter / map" - podstawowe operacje na strumieniach.
// W tej wersji wczytujemy dane strumieniowo prosto z pliku, bez tworzenia listy.
public
static
void
main
(
String
[]
args
)
{
ObslugaCSV
.
wczytajStrumieniowo
(
"emps.csv"
)
.
filter
(
emp
->
emp
.
getSalary
()
>=
10_000
)
.
map
(
emp
->
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
()
+
" "
+
emp
.
getSalary
())
.
map
(
String:
:
toUpperCase
)
// ew. .map(s -> s.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