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;
import java.util.Objects;
import java.util.function.BiConsumer;

public class Pair {
// TODO напишите реализацию
public class Pair<T1, T2> {
private final T1 firstValue;
private final T2 secondValue;

private Pair(T1 firstValue, T2 secondValue) {
this.firstValue = firstValue;
this.secondValue = secondValue;
}

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

public T1 getFirst() {
return firstValue;
}

public T2 getSecond() {
return secondValue;
}

public void ifPresent(BiConsumer biConsumer) {
if (this.firstValue != null && this.secondValue != null) biConsumer.accept(this.firstValue, this.secondValue);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || this.getClass() != o.getClass()) return false;
Pair<T1, T2> pair = (Pair<T1, T2>) o;
return this.firstValue.equals(pair.firstValue) && this.secondValue.equals(pair.secondValue);
}

@Override
public int hashCode() {
return Objects.hash(this.firstValue, this.secondValue);
}
}
5 changes: 3 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,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
39 changes: 32 additions & 7 deletions task02/src/com/example/task02/SavedList.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,60 @@
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;
import java.util.AbstractList;

public class SavedList<E extends Serializable> extends AbstractList<E> {

private List<E> savedList = new ArrayList<>();
private final File file;
public SavedList(File file) {
if (file.exists()){
try (ObjectInputStream objectInputStream = new ObjectInputStream(Files.newInputStream(file.toPath()))){
this.savedList = (List<E>) objectInputStream.readObject();
} catch (Exception e) {
e.printStackTrace();
}
}
this.file = file;
}

private void writeFile() {
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(Files.newOutputStream(file.toPath()))){
objectOutputStream.writeObject(this.savedList);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public E get(int index) {
return null;
return this.savedList.get(index);
}

@Override
public E set(int index, E element) {
return null;
E result = this.savedList.set(index, element);
this.writeFile();
return result;
}

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

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

@Override
public E remove(int index) {
return null;
E result = this.savedList.remove(index);
this.writeFile();
return result;
}
}
31 changes: 25 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,27 @@ public static void main(String[] args) throws IOException {
}

public static List<Set<String>> findAnagrams(InputStream inputStream, Charset charset) {
return null;
Map<String, Set<String>> treeMap = new TreeMap<>();

try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, charset))){
List<String> inputs = bufferedReader.lines()
.map(String::toLowerCase)
.filter(x -> x.matches("[а-я]+"))
.filter(x -> x.length() >= 3)
.collect(Collectors.toList());
for (String input : inputs) {
char[] chars = input.toCharArray();
Arrays.sort(chars);
String key = new String(chars);
treeMap.computeIfAbsent(key, set -> new TreeSet<>()).add(input);
}
} catch (Exception e) {
e.printStackTrace();
}

return treeMap.values()
.stream()
.filter(set -> set.size() >= 2)
.collect(Collectors.toList());
}
}