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
48 changes: 46 additions & 2 deletions task01/src/com/example/task01/Pair.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
package com.example.task01;

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

public class Pair<T, K> {
private final T first;
private final K second;

private Pair(T first, K second){
this.first = first;
this.second = second;
}

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

public T getFirst(){
return first;
}

public K getSecond(){
return second;
}

public void ifPresent(BiConsumer<? super T, ? super K> 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);
}
}
6 changes: 2 additions & 4 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,8 +20,7 @@ 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!
*/

System.out.println(mustBeTrue);
System.out.println(mustAlsoBeTrue);
}

}
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.
49 changes: 41 additions & 8 deletions task02/src/com/example/task02/SavedList.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,68 @@
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<E extends Serializable> extends AbstractList<E> {
private final File file;
private final List<E> 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<E> loadedList = (List<E>) stream.readObject();
list.clear();
list.addAll(loadedList);
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException("Error loading data from 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;
public E set(int index, E element){
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;
}
}
}
3 changes: 0 additions & 3 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
public class Task02Main {

public static void main(String[] args) throws IOException {

File file = new File("./testlist.dat");

SavedList<String> list1 = new SavedList<>(file);
Expand All @@ -15,7 +14,5 @@ public static void main(String[] args) throws IOException {

SavedList<String> list2 = new SavedList<>(file);
System.out.println("list2: " + list2);

}

}
38 changes: 30 additions & 8 deletions task03/src/com/example/task03/Task03Main.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -15,10 +13,34 @@ public static void main(String[] args) throws IOException {
for (Set<String> anagram : anagrams) {
System.out.println(anagram);
}

}

public static List<Set<String>> findAnagrams(InputStream inputStream, Charset charset) {
return null;
public static List<Set<String>> findAnagrams(
InputStream inputStream, Charset charset) {

Map<String, TreeSet<String>> result = new TreeMap<>();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream, charset))) {
List<String> 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());
}
}
Binary file added testlist.dat
Binary file not shown.