This commit is contained in:
vladyslav-crunch
2025-10-10 10:44:52 +02:00
commit 3b50f4a582
14 changed files with 523 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
package org.example;
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
public static void main(String[] args) {
// Press Alt+Enter with your caret at the highlighted text to see how
// IntelliJ IDEA suggests fixing it.
System.out.print("Hello and welcome!");
// Press Shift+F10 or click the green arrow button in the gutter to run the code.
for (int i = 1; i <= 5; i++) {
// Press Shift+F9 to start debugging your code. We have set one breakpoint
// for you, but you can always add more by pressing Ctrl+F8.
System.out.println("i = " + i);
}
}
}

View File

@@ -0,0 +1,64 @@
package org.example;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamExample {
public static void main(String[] args) throws IOException {
List<String> recipes = List.of(
"Pizza", "Spaghetti", "Burger", "Pizza", "Salad", "Burger", "Soup"
);
// 1⃣ Utwórz strumień i wyszukaj elementy poprzez zastosowanie filtra.
System.out.println("=== FILTROWANIE ===");
recipes.stream()
.filter(recipe -> recipe.startsWith("S"))
.forEach(System.out::println);
// 2⃣ Utwórz strumień i policz elementy powtarzające się i ile razy się powtarzają.
System.out.println("\n=== LICZENIE POWTÓRZEŃ ===");
Map<String, Long> counts = recipes.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
counts.forEach((key, value) ->
System.out.println(key + " występuje " + value + " raz/y"));
// 3⃣ Zastosuj mapowanie na strumieniu i utwórz nowy strumień powstały w jego wyniku.
System.out.println("\n=== MAPOWANIE ===");
List<String> upperRecipes = recipes.stream()
.map(String::toUpperCase)
.toList();
upperRecipes.forEach(System.out::println);
// 4⃣ Zapis i odczyt danych z pliku tekstowego (java.io + java.nio)
System.out.println("\n=== ZAPIS I ODCZYT Z PLIKU ===");
Path filePath = Paths.get("recipes.txt");
// Zapis (java.io)
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath.toFile()))) {
for (String recipe : recipes) {
writer.write(recipe);
writer.newLine();
}
}
// Odczyt (java.nio)
List<String> readRecipes = Files.readAllLines(filePath);
System.out.println("Odczytano z pliku:");
readRecipes.forEach(System.out::println);
// 5⃣ Filtrowanie i sortowanie danych zapisywanych lub odczytywanych z pliku.
System.out.println("\n=== FILTROWANIE I SORTOWANIE Z PLIKU ===");
List<String> filteredSorted = readRecipes.stream()
.filter(r -> r.length() > 5) // np. przepisy o nazwie >5 znaków
.sorted()
.toList();
System.out.println("Po filtrowaniu i sortowaniu:");
filteredSorted.forEach(System.out::println);
}
}