Skip to content
Merged

newMain #2384

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
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static Optional<Car> getCheapestCar(Stream<Car> cars) {
}

public static Optional<Car> getCheaperCar(Stream<Car> cars, Car cheapestCar) {
return cars.filter(x -> x.getPrice() < cheapestCar.getPrice()).findFirst();
return cars.filter(car -> car.getPrice() < cheapestCar.getPrice()).findFirst();
}
}

Expand Down
76 changes: 76 additions & 0 deletions 2.JavaCore/src/com/javarush/task/jdk13/task35/task3506/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.javarush.task.jdk13.task35.task3506;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
public static void main(String[] args) {
List<Animal> animals = new ArrayList<>(Arrays.asList(new Animal(1), new Animal(2)));
List<Pet> pets1 = new ArrayList<>(Arrays.asList(new Pet(3), new Pet(4)));
List<Pet> pets2 = new ArrayList<>(Arrays.asList(new Pet(5), new Pet(6)));
List<Cat> cats = new ArrayList<>(Arrays.asList(new Cat(7), new Cat(8)));

MySolution mySolution = new MySolution();
mySolution.one(pets1, pets2);
System.out.println(pets1);
System.out.println(pets2);

mySolution.two(pets2, cats);
System.out.println(pets2);
System.out.println(cats);

mySolution.three(animals, pets1);
System.out.println(animals);
System.out.println(pets1);

mySolution.four(animals, cats);
System.out.println(cats);
System.out.println(animals);

}
}

@Getter
@AllArgsConstructor
class Animal {
private int number;

@Override
public String toString() {
return Animal.class.getSimpleName() + "{" +
"number=" + number +
'}';
}
}

class Pet extends Animal {

public Pet(int number) {
super(number);
}

@Override
public String toString() {
return Pet.class.getSimpleName() + "{" +
"number=" + getNumber() +
'}';
}
}

class Cat extends Pet {

public Cat(int number) {
super(number);
}

@Override
public String toString() {
return Cat.class.getSimpleName() + "{" +
"number=" + getNumber() +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.javarush.task.jdk13.task35.task3506;

import java.util.List;

public class MySolution extends Solution{
@Override
public <T> void one(List<T> destination, List<T> source) {
destination.addAll(source);
}

@Override
public <T> void two(List<T> destination, List<? extends T> source) {
destination.addAll(source);
}

@Override
public <T> void three(List<? super T> destination, List<T> source) {
destination.addAll(source);
}

@Override
public <T> void four(List<? super T> destination, List<? extends T> source) {
destination.addAll(source);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

### extends vs super

Логика всех методов - добавить source в destination.
Логика всех методов - добавить source в destination.\
!!!Расставь ?, extends и super где необходимо:!!!
1) one - должен работать с одним и тем же типом;
2) two - должен добавлять любых наследников типа T в список, умеющий хранить только тип T;
3) three - должен добавлять объекты типа T в любой список, параметризированный любым родительским классом;
4) four - должен добавлять любых наследников типа T в список, параметризированный любым родительским классом.
4) four - должен добавлять любых наследников типа T в список, параметризированный любым родительским классом.\
Не оставляй закомментированный код.


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@ public static <K, V> HashMap<K, V> newHashMap(List<? extends K> keys, List<? ext

return map;
// return IntStream.range(0, keys.size()).boxed().collect(Collectors.toMap(keys::get, values::get, (a, b) -> b, HashMap::new));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ public FileConsoleWriter(FileDescriptor fd) {
fileWriter = new FileWriter(fd);
}

public static void main(String[] args) {

}

public void write(char[] cbuf, int off, int len) throws IOException {
fileWriter.write(cbuf, off, len);
System.out.println(new String(cbuf, off, len));
}

public void write(int c) throws IOException {
fileWriter.write(c);
System.out.println((char) c);
Expand All @@ -61,5 +58,7 @@ public void close() throws IOException {
fileWriter.close();
}

public static void main(String[] args) {

}
}
104 changes: 103 additions & 1 deletion 2.JavaCore/src/com/javarush/task/task19/task1918/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

/*
Знакомство с тегами
Expand All @@ -21,13 +25,111 @@ public static void main(String[] args) throws IOException {

try (BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
BufferedReader reader = new BufferedReader(new FileReader(console.readLine()))) {

while (reader.ready()) {
builder.append(reader.readLine());
}

Document document = Jsoup.parse(builder.toString(), "", Parser.xmlParser());
Elements elements = document.select(args[0]);
elements.forEach(System.out::println);

}
}

public static void main1(String[] args) throws IOException {
try (BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
BufferedReader bufferedReader = new BufferedReader((new FileReader(console.readLine())))) {
StringBuilder stringBuilder = new StringBuilder();

while (bufferedReader.ready()) {
stringBuilder.append(bufferedReader.readLine());
}

String line = stringBuilder.toString();

int tagIndex = line.indexOf("<" + args[0]);
List<Integer> tagList = new ArrayList<>();

while (tagIndex != -1) {
tagList.add(tagIndex);

tagIndex = line.indexOf("<" + args[0], tagIndex + 1);
}

for (Integer startIndex : tagList) {
int lastOpenTagIndex = startIndex;
int lastCloseTagIndex = line.indexOf("</" + args[0], lastOpenTagIndex);

while (true) {
int next = line.indexOf("<" + args[0], lastOpenTagIndex + 1);
if (next < lastCloseTagIndex && lastOpenTagIndex < next) {
lastOpenTagIndex = line.indexOf("<" + args[0], lastOpenTagIndex + 1);
lastCloseTagIndex = line.indexOf("</" + args[0], lastCloseTagIndex + 1);
} else {
System.out.println(line.substring(startIndex, lastCloseTagIndex + 3 + args[0].length()));
break;
}
}
}
}
}

public static void main2(String[] args) {
String fileName = null;
try (BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in))) {
fileName = consoleReader.readLine();
} catch (IOException ignore) {
}

StringBuilder readFileContent = new StringBuilder();
try (BufferedReader fileReader = new BufferedReader(new FileReader(fileName))) {
while (fileReader.ready()) {
readFileContent.append(fileReader.readLine());
}
} catch (IOException ignore) {
}

String fileContent = readFileContent.toString().replaceAll("[\\r\\n]+", "");

String tag = args[0];
String openedTag = "<" + tag;
String closedTag = "</" + tag;
int openedTagIndex = fileContent.indexOf(openedTag);
int closedTagIndex = fileContent.indexOf(closedTag);
ArrayList<Integer> openedTagsIndexes = new ArrayList<>();
ArrayList<Integer> closedTagsIndexes = new ArrayList<>();

while (openedTagIndex != -1 || closedTagIndex != -1) {
if (openedTagIndex != -1 && openedTagIndex < closedTagIndex) {
openedTagsIndexes.add(openedTagIndex);
openedTagIndex = fileContent.indexOf(openedTag, openedTagIndex + 1);
} else if (closedTagIndex < openedTagIndex || openedTagIndex == -1) {
closedTagsIndexes.add(closedTagIndex + tag.length() + 3);
closedTagIndex = fileContent.indexOf(closedTag, closedTagIndex + 1);
}
}

Stack<String> stack = new Stack<>();
for (int i = openedTagsIndexes.size() - 1; i >= 0; i--) {
stack.push(fileContent.substring(openedTagsIndexes.get(i), getNextCloseTag(closedTagsIndexes, openedTagsIndexes.get(i))));
}

while (stack.size() > 0) {
System.out.println(stack.pop());
}
}
}

private static int getNextCloseTag(ArrayList<Integer> closedTagsIndexes, Integer openTagIndex) {
Iterator<Integer> iterator = closedTagsIndexes.iterator();
while (iterator.hasNext()) {
Integer next = iterator.next();
if (next > openTagIndex) {
iterator.remove();
return next;
}
}
return 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@
### Знакомство с тегами

Считай с консоли имя файла, который имеет HTML-формат.

Пример:

Info about Leela &lt;span xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;&lt;b&gt;&lt;span&gt;Turanga Leela
&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;span&gt;Super&lt;/span&gt;&lt;span&gt;girl&lt;/span&gt;
Первым параметром в метод main приходит тег. Например, &quot;span&quot;.
Вывести на консоль все теги, которые соответствуют заданному тегу.

Первым параметром в метод main приходит тег. Например, &quot;span&quot;.\
Вывести на консоль все теги, которые соответствуют заданному тегу.\
Каждый тег на новой строке, порядок должен соответствовать порядку следования в файле.
Количество пробелов, \n, \r не влияют на результат.
Количество пробелов, \n, \r не влияют на результат.\
Файл не содержит тег CDATA, для всех открывающих тегов имеется отдельный закрывающий тег, одиночных тегов нет.
Тег может содержать вложенные теги.

Пример вывода:

&lt;span xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;&lt;b&gt;&lt;span&gt;Turanga Leela&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;
&lt;span&gt;Turanga Leela&lt;/span&gt;
&lt;span&gt;Super&lt;/span&gt;
&lt;span&gt;girl&lt;/span&gt;

Шаблон тега:

&lt;tag&gt;text1&lt;/tag&gt;
&lt;tag text2&gt;text1&lt;/tag&gt;
&lt;tag
Expand Down
32 changes: 19 additions & 13 deletions 2.JavaCore/src/com/javarush/task/task19/task1924/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.IntStream;

/*
Замена чисел
Expand All @@ -12,19 +13,24 @@ public class Solution {
public static Map<Integer, String> map = new HashMap<>();

static {
map.put(0, "ноль");
map.put(1, "один");
map.put(2, "два");
map.put(3, "три");
map.put(4, "четыре");
map.put(5, "пять");
map.put(6, "шесть");
map.put(7, "семь");
map.put(8, "восемь");
map.put(9, "девять");
map.put(10, "десять");
map.put(11, "одиннадцать");
map.put(12, "двенадцать");
String[] array = {"ноль", "один", "два", "три", "четыре", "пять", "шесть",
"семь", "восемь", "девять", "десять", "одиннадцать", "двенадцать"};

IntStream.range(0, array.length).forEach(i -> map.put(i, array[i]));

// map.put(0, "ноль");
// map.put(1, "один");
// map.put(2, "два");
// map.put(3, "три");
// map.put(4, "четыре");
// map.put(5, "пять");
// map.put(6, "шесть");
// map.put(7, "семь");
// map.put(8, "восемь");
// map.put(9, "девять");
// map.put(10, "десять");
// map.put(11, "одиннадцать");
// map.put(12, "двенадцать");
}

public static void main(String[] args) throws IOException {
Expand Down