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

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

public class Pair<F, S> {
private final F first;
private final S second;

private Pair(F first, S second) {
this.first = first;
this.second = second;
}

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

public F getFirst() {
return first;
}

public S getSecond() {
return second;
}

public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;

Pair<?, ?> pair = (Pair<?, ?>) obj;

return Objects.equals(first, pair.first) && Objects.equals(second, pair.second);
}

public int hashCode() {
return Objects.hash(first, second);
}

public void ifPresent(BiConsumer<? super F, ? super S> biConsumer) {
if (first != null && second != null) biConsumer.accept(first, second);
}
}
38 changes: 32 additions & 6 deletions task02/src/com/example/task02/SavedList.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,61 @@
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 = (List<E>) is.readObject();
} catch (Exception e) {
}
} else {
list = new ArrayList<>();
}
}

private void writeFile() {
try (ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream(file))) {
obj.writeObject(list);
} catch (IOException e) {
}
}

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

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

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

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

@Override
public E remove(int index) {
return null;
E old = list.remove(index);
this.writeFile();
return old;
}
}
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>> anagrams = new TreeMap<>();

try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
for (String line; (line = reader.readLine()) != null; ) {
String[] words = line.toLowerCase().split("\\s");

for (String word : words) {
if (word.length() >= 3 && word.matches("[а-я]+")) {
char[] chars = word.toCharArray();
Arrays.sort(chars);
String sortedWord = new String(chars);

anagrams.computeIfAbsent(sortedWord, k -> new TreeSet<>()).add(word);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}

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