diff --git a/task01/src/com/example/task01/Pair.java b/task01/src/com/example/task01/Pair.java index f6fb603b..c4256c46 100644 --- a/task01/src/com/example/task01/Pair.java +++ b/task01/src/com/example/task01/Pair.java @@ -1,5 +1,44 @@ package com.example.task01; -public class Pair { - // TODO напишите реализацию +import java.util.Objects; +import java.util.function.BiConsumer; + +public class Pair { + private final T1 first; + private final T2 second; + + private Pair(T1 first, T2 second) { + this.first = first; + this.second = second; + } + + public static Pair of(T1 first, T2 second) { + return new Pair<>(first, second); + } + + public T1 getFirst() { + return first; + } + + public T2 getSecond() { + return second; + } + + public void ifPresent(BiConsumer consumer) { + if (first != null && second != null) + consumer.accept(first, second); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Pair pair = (Pair) o; + return Objects.equals(first, pair.first) && Objects.equals(second, pair.second); + } + + @Override + public int hashCode() { + return Objects.hash(first, second); + } } diff --git a/task01/src/com/example/task01/Task01Main.java b/task01/src/com/example/task01/Task01Main.java index 54dde94f..aaaaba8e 100644 --- a/task01/src/com/example/task01/Task01Main.java +++ b/task01/src/com/example/task01/Task01Main.java @@ -8,7 +8,6 @@ public static void main(String[] args) throws IOException { // TODO С корректно реализованным классом Pair должен компилироваться и успешно работать следующий код: - /* Pair pair = Pair.of(1, "hello"); Integer i = pair.getFirst(); // 1 String s = pair.getSecond(); // "hello" @@ -21,7 +20,6 @@ public static void main(String[] args) throws IOException { Pair pair2 = Pair.of(1, "hello"); boolean mustBeTrue = pair.equals(pair2); // true! boolean mustAlsoBeTrue = pair.hashCode() == pair2.hashCode(); // true! - */ } diff --git a/task02/list1.dat b/task02/list1.dat new file mode 100644 index 00000000..e09632d3 Binary files /dev/null and b/task02/list1.dat differ diff --git a/task02/list1a.dat b/task02/list1a.dat new file mode 100644 index 00000000..e23b42f9 Binary files /dev/null and b/task02/list1a.dat differ diff --git a/task02/list2.dat b/task02/list2.dat new file mode 100644 index 00000000..5f09bec6 Binary files /dev/null and b/task02/list2.dat differ diff --git a/task02/list4.dat b/task02/list4.dat new file mode 100644 index 00000000..76e4dede Binary files /dev/null and b/task02/list4.dat differ diff --git a/task02/src/com/example/task02/SavedList.java b/task02/src/com/example/task02/SavedList.java index 6b3a037d..d3b68f33 100644 --- a/task02/src/com/example/task02/SavedList.java +++ b/task02/src/com/example/task02/SavedList.java @@ -1,35 +1,70 @@ package com.example.task02; -import java.io.File; -import java.io.Serializable; +import java.io.*; import java.util.AbstractList; +import java.util.ArrayList; +import java.util.List; -public class SavedList extends AbstractList { +public class SavedList extends AbstractList { + private final File file; + private final List list; public SavedList(File file) { + this.file = file; + this.list = new ArrayList<>(); + + if (file.exists() && file.length() > 0) { + try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) { + List loadedList = (List) ois.readObject(); + list.addAll(loadedList); + } catch (IOException | ClassNotFoundException e) { + System.err.println("Ошибка загрузки: " + e.getMessage()); + } + } + } + + private void saveToFile() { + try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) { + oos.writeObject(new ArrayList<>(list)); + } catch (IOException e) { + System.err.println("Ошибка сохранения: " + e.getMessage()); + } } @Override - public E get(int index) { - return null; + public T get(int index) { + return list.get(index); } @Override - public E set(int index, E element) { - return null; + public T set(int index, T element) { + T oldElement = list.set(index, element); + saveToFile(); + return oldElement; } @Override public int size() { - return 0; + return list.size(); + } + + @Override + public void add(int index, T element) { + list.add(index, element); + saveToFile(); } @Override - public void add(int index, E element) { + public boolean add(T element) { + boolean result = list.add(element); + saveToFile(); + return result; } @Override - public E remove(int index) { - return null; + public T remove(int index) { + T removedElement = list.remove(index); + saveToFile(); + return removedElement; } } diff --git a/task03/src/com/example/task03/Task03Main.java b/task03/src/com/example/task03/Task03Main.java index 7f255e98..115e41f5 100644 --- a/task03/src/com/example/task03/Task03Main.java +++ b/task03/src/com/example/task03/Task03Main.java @@ -1,11 +1,8 @@ package com.example.task03; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.nio.charset.Charset; -import java.util.List; -import java.util.Set; +import java.util.*; public class Task03Main { @@ -19,6 +16,58 @@ public static void main(String[] args) throws IOException { } public static List> findAnagrams(InputStream inputStream, Charset charset) { - return null; + if (!charset.isRegistered()) { + throw new IllegalArgumentException(); + } + + Map> map = new HashMap<>(); + Set uniqueWords = new HashSet<>(); + + try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charset))) { + String line; + while ((line = br.readLine()) != null) { + line = line.trim().toLowerCase(); + + if (isValidLine(line)) { + uniqueWords.add(line); + } + } + } catch (IOException ioe) { + ioe.printStackTrace(); + } + + for (String word : uniqueWords) { + char[] chars = word.toCharArray(); + Arrays.sort(chars); + String key = new String(chars); + + map.computeIfAbsent(key, k -> new TreeSet<>()).add(word); + } + + List> result = new ArrayList<>(); + + for (Set group : map.values()) { + if (group.size() >= 2) { + result.add(group); + } + } + + result.sort(Comparator.comparing(set -> set.iterator().next())); + + return result; + } + + private static boolean isValidLine(String line) { + if (line.length() < 3) { + return false; + } + + for (char ch : line.toCharArray()) { + if (!((ch >= 'а' && ch <= 'я') || ch == 'ё')) { + return false; + } + } + + return true; } }