diff --git a/task01/src/com/example/task01/Pair.java b/task01/src/com/example/task01/Pair.java index f6fb603b..680c0cfa 100644 --- a/task01/src/com/example/task01/Pair.java +++ b/task01/src/com/example/task01/Pair.java @@ -1,5 +1,43 @@ package com.example.task01; -public class Pair { - // TODO напишите реализацию +import java.util.Objects; +import java.util.function.BiConsumer; + +public class Pair { + private final T first; + private final U second; + + private Pair(T first, U second) { + this.first = first; + this.second = second; + } + + public static Pair of(T first, U second) { + return new Pair<>(first, second); + } + + public T getFirst() { + return first; + } + + public U getSecond() { + return second; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + Pair pair = (Pair) obj; + return Objects.equals(first, pair.first) && Objects.equals(second, pair.second); + } + + @Override + public int hashCode() { + return Objects.hash(first, second); + } + + public void ifPresent(BiConsumer biConsumer) { + if (first != null && second != null) biConsumer.accept(first, second); + } } diff --git a/task01/src/com/example/task01/Task01Main.java b/task01/src/com/example/task01/Task01Main.java index 54dde94f..466e87fd 100644 --- a/task01/src/com/example/task01/Task01Main.java +++ b/task01/src/com/example/task01/Task01Main.java @@ -6,9 +6,6 @@ public class Task01Main { 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,8 +18,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..4a72faab 100644 --- a/task02/src/com/example/task02/SavedList.java +++ b/task02/src/com/example/task02/SavedList.java @@ -1,35 +1,67 @@ package com.example.task02; -import java.io.File; -import java.io.Serializable; +import java.io.*; +import java.nio.file.Files; import java.util.AbstractList; +import java.util.ArrayList; +import java.util.List; public class SavedList extends AbstractList { + private final File file; + private final List list = new ArrayList<>(); public SavedList(File file) { + this.file = file; + loadFromFile(); + } + + private void loadFromFile() { + if (!file.exists()) return; + try (ObjectInputStream stream = new ObjectInputStream(Files.newInputStream(file.toPath()))) { + @SuppressWarnings("unchecked") + List loadedList = (ArrayList) stream.readObject(); + list.clear(); + list.addAll(loadedList); + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException("Error downloading data from a file", e); + } + } + + private void saveToFile() { + try (ObjectOutputStream stream = new ObjectOutputStream(Files.newOutputStream(file.toPath()))) { + stream.writeObject(list); + } catch (IOException e) { + throw new RuntimeException("Error saving data to file", e); + } } @Override public E get(int index) { - return null; + return list.get(index); } @Override public E set(int index, E element) { - return null; + E oldElement = list.set(index, element); + saveToFile(); + return oldElement; } @Override public int size() { - return 0; + return list.size(); } @Override public void add(int index, E element) { + list.add(index, element); + saveToFile(); } @Override public E remove(int index) { - return null; + E removedElement = list.remove(index); + saveToFile(); + return removedElement; } -} +} \ No newline at end of file diff --git a/task03/src/com/example/task03/Task03Main.java b/task03/src/com/example/task03/Task03Main.java index 7f255e98..19c049d6 100644 --- a/task03/src/com/example/task03/Task03Main.java +++ b/task03/src/com/example/task03/Task03Main.java @@ -1,11 +1,9 @@ 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.*; +import java.util.stream.Collectors; public class Task03Main { @@ -15,10 +13,25 @@ public static void main(String[] args) throws IOException { for (Set anagram : anagrams) { System.out.println(anagram); } - } public static List> findAnagrams(InputStream inputStream, Charset charset) { - return null; + Map> result = new TreeMap<>(); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))) { + List lines = reader.lines() + .map(String::toLowerCase) + .filter(str -> str.matches("[а-яё]+") && str.length() >= 3) + .collect(Collectors.toList()); + for (String line : lines) { + char[] symbols = line.toCharArray(); + Arrays.sort(symbols); + String key = new String(symbols); + result.computeIfAbsent(key, value -> new TreeSet<>()).add(line); + } + } catch (IOException e) { } + return result.values() + .stream() + .filter(value -> value.size() >= 2) + .collect(Collectors.toList()); } -} +} \ No newline at end of file diff --git a/testlist.dat b/testlist.dat new file mode 100644 index 00000000..c73617b1 Binary files /dev/null and b/testlist.dat differ