Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions task01/src/com/example/task01/Pair.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
package com.example.task01;

public class Pair {
// TODO напишите реализацию
import java.util.Objects;
import java.util.function.BiConsumer;

public class Pair<T1, T2> {
private final T1 first;
private final T2 second;

private Pair(T1 first, T2 second) {
this.first = first;
this.second = second;
}

public static <T1, T2> Pair<T1, T2> of(T1 first, T2 second) {
return new Pair<>(first, second);
}

public T1 getFirst() {
return first;
}

public T2 getSecond() {
return second;
}

public void ifPresent(BiConsumer<? super T1,? super T2> 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);
}
}
2 changes: 0 additions & 2 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public static void main(String[] args) throws IOException {

// TODO С корректно реализованным классом Pair должен компилироваться и успешно работать следующий код:

/*
Pair<Integer, String> pair = Pair.of(1, "hello");
Integer i = pair.getFirst(); // 1
String s = pair.getSecond(); // "hello"
Expand All @@ -21,7 +20,6 @@ public static void main(String[] args) throws IOException {
Pair<Integer, String> pair2 = Pair.of(1, "hello");
boolean mustBeTrue = pair.equals(pair2); // true!
boolean mustAlsoBeTrue = pair.hashCode() == pair2.hashCode(); // true!
*/

}

Expand Down
Binary file added task02/list1.dat
Binary file not shown.
Binary file added task02/list1a.dat
Binary file not shown.
Binary file added task02/list2.dat
Binary file not shown.
Binary file added task02/list4.dat
Binary file not shown.
57 changes: 46 additions & 11 deletions task02/src/com/example/task02/SavedList.java
Original file line number Diff line number Diff line change
@@ -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<E extends Serializable> extends AbstractList<E> {
public class SavedList<T extends Serializable> extends AbstractList<T> {
private final File file;
private final List<T> 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<T> loadedList = (List<T>) 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;
}
}
61 changes: 55 additions & 6 deletions task03/src/com/example/task03/Task03Main.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -19,6 +16,58 @@ public static void main(String[] args) throws IOException {
}

public static List<Set<String>> findAnagrams(InputStream inputStream, Charset charset) {
return null;
if (!charset.isRegistered()) {
throw new IllegalArgumentException();
}

Map<String, Set<String>> map = new HashMap<>();
Set<String> 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<Set<String>> result = new ArrayList<>();

for (Set<String> 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;
}
}