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

public class Pair {
// TODO напишите реализацию
import java.util.Objects;
import java.util.function.BiConsumer;
public class Pair<F, S> {
private final F FirstValue;
private final S SecondValue;

private Pair(F firstValue, S secondValue) {
FirstValue = firstValue;
SecondValue = secondValue;
}

public static <F, S> Pair<F, S> of(F firstValue, S secondValue) {
return new Pair<>(firstValue, secondValue);
}

public F getFirst() {
return FirstValue;
}

public S getSecond() {
return SecondValue;
}

public void ifPresent(BiConsumer<? super F, ? super S> biConsumer) {
if (FirstValue != null && SecondValue != null) biConsumer.accept(FirstValue, SecondValue);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || this.getClass() != obj.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) obj;
return Objects.equals(FirstValue, pair.FirstValue) && Objects.equals(SecondValue, pair.SecondValue);
}

@Override
public int hashCode() {
return Objects.hash(FirstValue, SecondValue);
}
}
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 @@ -8,7 +8,7 @@ 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 +21,9 @@ 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
37 changes: 29 additions & 8 deletions task02/src/com/example/task02/SavedList.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,56 @@
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> {
private final File file;
private List<E> list;

public SavedList(File file) {
this.file = file;
if (file.exists()) {
try (ObjectInputStream is = new ObjectInputStream(new FileInputStream(file))) {
list = (ArrayList<E>) is.readObject();
} catch (Exception ignored) { }
} else {
list = new ArrayList<>();
}
}

@Override
public E get(int index) {
return null;
return list.get(index);
}

@Override
public E set(int index, E element) {
return null;
E el = list.set(index, element);
writeFile();
return el;
}

@Override
public int size() {
return 0;
}
public int size() {return list.size();}

@Override
public void add(int index, E element) {
list.add(index, element);
writeFile();
}

@Override
public E remove(int index) {
return null;
E el = list.remove(index);
writeFile();
return el;
}
private void writeFile() {
try (ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream(file))) {
obj.writeObject(list);
} catch (IOException ignored) {
}
}
}
27 changes: 21 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,23 @@ 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 br = new BufferedReader(new InputStreamReader(inputStream, charset))) {
List<String> entries = br.lines()
.map(String::toLowerCase)
.filter(x -> x.matches("[а-яё]+") && x.length() >= 3)
.collect(Collectors.toList());
for (String s : entries) {
char[] symbols = s.toCharArray();
Arrays.sort(symbols);
String key = new String(symbols);
result.computeIfAbsent(key, a -> new TreeSet<>()).add(s);
}
} catch (IOException ignored) { }
return result.values()
.stream()
.filter(x -> x.size() >= 2)
.collect(Collectors.toList());
}

}