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
50 changes: 48 additions & 2 deletions task01/src/com/example/task01/Pair.java
Original file line number Diff line number Diff line change
@@ -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<T, K> {
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 <T, K> Pair<T, K> of(T first, K second){
return new Pair<>(first, 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: 4 additions & 2 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, String> pair = Pair.of(1, "hello");
Integer i = pair.getFirst(); // 1
String s = pair.getSecond(); // "hello"
Expand All @@ -21,7 +22,8 @@ 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);

}

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.
46 changes: 40 additions & 6 deletions task02/src/com/example/task02/SavedList.java
Original file line number Diff line number Diff line change
@@ -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<E extends Serializable> extends AbstractList<E> {
private final File file;
private List<E> 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<E>) 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;
}
}
35 changes: 29 additions & 6 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 @@ -19,6 +17,31 @@ public static void main(String[] args) throws IOException {
}

public static List<Set<String>> findAnagrams(InputStream inputStream, Charset charset) {
return null;
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.