diff --git a/task01/src/com/example/task01/Pair.java b/task01/src/com/example/task01/Pair.java index f6fb603b..1912e2b4 100644 --- a/task01/src/com/example/task01/Pair.java +++ b/task01/src/com/example/task01/Pair.java @@ -1,5 +1,51 @@ package com.example.task01; -public class Pair { - // TODO напишите реализацию +import java.util.Objects; +import java.util.Optional; +import java.util.function.BiConsumer; +import java.util.function.Consumer; + +public class Pair { + private final T first; + private final K second; + + private Pair(T first, K second){ + this.first = first; + this.second = second; + } + + public T getFirst(){ + return first; + } + + public K getSecond(){ + return second; + } + + public static Pair of(T first, K second){ + return new Pair<>(first, second); + } + + public void ifPresent(BiConsumer consumer) { + if (first != null && second != null) + consumer.accept(first, second); + } + + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (!(obj instanceof Pair)) { + return false; + } + + Pair other = (Pair) obj; + return Objects.equals(first, other.first) && Objects.equals(second, other.second); + } + + 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..dc1a1285 100644 --- a/task01/src/com/example/task01/Task01Main.java +++ b/task01/src/com/example/task01/Task01Main.java @@ -2,13 +2,14 @@ import java.io.IOException; import java.io.InputStream; +import java.util.Optional; 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,7 +22,8 @@ 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! - */ + System.out.println(mustBeTrue); + System.out.println(mustAlsoBeTrue); } 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..c77aae6e 100644 --- a/task02/src/com/example/task02/SavedList.java +++ b/task02/src/com/example/task02/SavedList.java @@ -1,35 +1,69 @@ package com.example.task02; -import java.io.File; -import java.io.Serializable; +import javax.xml.bind.Element; +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 List savedList = new ArrayList<>(); public SavedList(File file) { + this.file = file; + + if (file.exists() && file.isFile()) { + try (ObjectInputStream objectInputStream = new ObjectInputStream( + Files.newInputStream(file.toPath()))) { + + savedList = (List) objectInputStream.readObject(); + + + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException("Ошибка загрузки данных", e); + } + } } + private void writeToFile() { + try (ObjectOutputStream stream = new ObjectOutputStream(Files.newOutputStream(file.toPath()))) { + stream.writeObject(savedList); + } catch (IOException e) { + throw new RuntimeException("Ошибка записи данных", e); + } + } + + + @Override public E get(int index) { - return null; + return savedList.get(index); } @Override public E set(int index, E element) { - return null; + E Element = savedList.set(index, element); + writeToFile(); + return Element; } @Override public int size() { - return 0; + return savedList.size(); } @Override public void add(int index, E element) { + savedList.add(index, element); + writeToFile(); } @Override public E remove(int index) { - return null; + E Element = savedList.remove(index); + writeToFile(); + return Element; } } diff --git a/task03/src/com/example/task03/Task03Main.java b/task03/src/com/example/task03/Task03Main.java index 7f255e98..0f041fc4 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 { @@ -19,6 +17,31 @@ public static void main(String[] args) throws IOException { } 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) + .distinct() + .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 (Exception e) { + return new ArrayList<>(); + } + + return result.values() + .stream() + .filter(value -> value.size() >= 2) + .collect(Collectors.toList()); } + + + } diff --git a/testlist.dat b/testlist.dat new file mode 100644 index 00000000..a438cb56 Binary files /dev/null and b/testlist.dat differ