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
9 changes: 5 additions & 4 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.task01;

import java.io.IOException;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;

Expand All @@ -9,21 +10,21 @@ public static void main(String[] args) throws IOException {

// TODO С корректно реализованным методом ternaryOperator должен компилироваться и успешно работать следующий код:

/*

Predicate<Object> condition = Objects::isNull;
Function<Object, Integer> ifTrue = obj -> 0;
Function<CharSequence, Integer> ifFalse = CharSequence::length;
Function<String, Integer> safeStringLength = ternaryOperator(condition, ifTrue, ifFalse);
*/

}

public static <T, U> Function<T, U> ternaryOperator(
Predicate<? super T> condition,
Function<? super T, ? extends U> ifTrue,
Function<? super T, ? extends U> ifFalse) {

return null; // your implementation here
if (condition == null || ifTrue == null || ifFalse == null)
throw new NullPointerException();

return x -> condition.test(x) ? ifTrue.apply(x) : ifFalse.apply(x);
}
}
12 changes: 6 additions & 6 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
public class Task02Main {

public static void main(String[] args) {

/*
cycleGrayCode(2)
.limit(10)
.forEach(System.out::println);
*/

}

public static IntStream cycleGrayCode(int n) {
if (n < 1 || n > 16) { throw new IllegalArgumentException(); }
int size = 1 << n;

return null; // your implementation here

return IntStream
.iterate(0, i -> i + 1)
.map(i -> i % size)
.map(i -> i ^ (i >> 1));
}

}
4 changes: 3 additions & 1 deletion task03/src/com/example/task03/Task03Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public static <T> void findMinMax(
Stream<? extends T> stream,
Comparator<? super T> order,
BiConsumer<? super T, ? super T> minMaxConsumer) {
try (Stream ){

// your implementation here
}
//return x -> stream.min(order);
}
}
30 changes: 27 additions & 3 deletions task04/src/com/example/task04/Task04Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
package com.example.task04;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

public class Task04Main {

public static void main(String[] args) {
public static void main(String[] args) throws IOException {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in, "UTF-8"))) {

// your implementation here
String text = reader.lines().collect(Collectors.joining(" "));

}
String result = java.util.Arrays.stream(text.split("[^\\p{L}\\d]+"))
.filter(word -> !word.isEmpty())
.map(String::toLowerCase)
.collect(Collectors.groupingBy(w -> w, Collectors.counting()))
.entrySet()
.stream()
.sorted(
Map.Entry.<String, Long>comparingByValue().reversed()
.thenComparing(Map.Entry.comparingByKey())
)
.limit(10)
.map(Map.Entry::getKey)
.collect(Collectors.joining("\n"));

System.out.println(result);
}
}
}
7 changes: 7 additions & 0 deletions task05/src/com/example/task05/MailItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.task05;

public interface MailItem <T>{
String getFrom();
String getTo();
T getContent();
}
28 changes: 28 additions & 0 deletions task05/src/com/example/task05/MailMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.task05;

public class MailMessage implements MailItem<String>{

private final String from;
private final String to;
private final String content;
public MailMessage(String from, String to, String content) {
this.from = from;
this.to = to;
this.content = content;
}

@Override
public String getFrom() {
return from;
}

@Override
public String getTo() {
return to;
}

@Override
public String getContent() {
return content;
}
}
31 changes: 31 additions & 0 deletions task05/src/com/example/task05/MailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.task05;

import java.util.*;
import java.util.function.Consumer;

public class MailService<T> implements Consumer<MailItem<T>> {
private final Map<String, List<T>> mailBox = new HashMap<>();

@Override
public void accept(MailItem<T> mailItem) {
if (mailItem == null) return;
String to = mailItem.getTo();
T content = mailItem.getContent();
if (to == null || content == null) return;
mailBox.computeIfAbsent(to, k -> new ArrayList<>()).add(content);
}

public Map<String, List<T>> getMailBox() {
return new AbstractMap<String, List<T>>() {
@Override
public Set<Entry<String, List<T>>> entrySet() {
return mailBox.entrySet();
}
@Override
public List<T> get(Object key) {
List<T> list = mailBox.get(key);
return list != null ? list : Collections.emptyList();
}
};
}
}
30 changes: 30 additions & 0 deletions task05/src/com/example/task05/Salary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.task05;

public class Salary implements MailItem<Integer> {

private final String from;
private final String to;
private final Integer amount;


public Salary(String from, String to, Integer amount) {
this.from = from;
this.to = to;
this.amount = amount;
}

@Override
public String getFrom() {
return from;
}

@Override
public String getTo() {
return to;
}

@Override
public Integer getContent() {
return amount;
}
}
9 changes: 2 additions & 7 deletions task05/src/com/example/task05/Task05Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ public class Task05Main {

public static void main(String[] args) {

/*

// Random variables
String randomFrom = "..."; // Некоторая случайная строка. Можете выбрать ее самостоятельно.
String randomTo = "..."; // Некоторая случайная строка. Можете выбрать ее самостоятельно.
String randomFrom = "one"; // Некоторая случайная строка. Можете выбрать ее самостоятельно.
String randomTo = "ten"; // Некоторая случайная строка. Можете выбрать ее самостоятельно.
int randomSalary = 100; // Некоторое случайное целое положительное число. Можете выбрать его самостоятельно.

// Создание списка из трех почтовых сообщений.
Expand Down Expand Up @@ -88,8 +85,6 @@ public static void main(String[] args) {
assert salaries.get(randomTo).equals(Arrays.asList(randomSalary)) : "wrong salaries mailbox content (3)";


*/

}

}