Java 26.11.2023

This commit is contained in:
StNicolay 2023-11-26 18:03:07 +00:00
parent b40362f28b
commit 352cfb4cdf

View File

@ -894,7 +894,7 @@ class PersonAgeComparator implements Comparator<Person> {
} }
class Main { class Main {
public static void Main(String[] args) { public static void main(String[] args) {
Comparator<Person> pcomp = (new PersonNameComparator()).thenComparing(new PersonAgeComparator()); Comparator<Person> pcomp = (new PersonNameComparator()).thenComparing(new PersonAgeComparator());
TreeSet<Person> people = new TreeSet<Person>(pcomp); TreeSet<Person> people = new TreeSet<Person>(pcomp);
people.add(new Person("Tom", 10)); people.add(new Person("Tom", 10));
@ -915,7 +915,7 @@ class Main {
```Java ```Java
public class Main { public class Main {
public static void Main(String[] args) { public static void main(String[] args) {
Operation op = (x, y) -> x + y; Operation op = (x, y) -> x + y;
Operation op2 = (int x, int y) -> x - y; Operation op2 = (int x, int y) -> x - y;
@ -944,10 +944,111 @@ interface Printer {
} }
class Main { class Main {
public static void Main(String[] args) { public static void main(String[] args) {
Printer p = (s) -> System.out.println(s); Printer p = (s) -> System.out.println(s);
} }
} }
``` ```
Переменные, которые используются в лямбде нельзя менять Переменные, которые используются в лямбде нельзя менять
### Блоки кода в лямбда-функциях
```Java
public class Main {
public static void main(String[] args) {
Operation op = (int x, int y) -> {
if(x < 0) {
x = -x;
}
return x + y;
};
}
}
interface Operation {
int calculate(int x, int y);
}
```
### Обобщённые лямбды
```Java
public class Main {
public static void main(String[] args) {
Operation<Integer> op = (x, y) -> {
if(x < 0) {
x = -x;
}
return x + y;
};
}
}
interface Operation<T> {
T calculate(T x, T y);
}
```
## Ссылки на метод, как параметр методов
```Java
interface Operation {
int calculate(int x, int y);
}
class Operations {
static int Add(int x, int y) {
return x + y;
}
}
class Main {
public static void main(String[] args) {
Operation op = Operations::Add;
}
}
```
Также можно ссылаться на методы объекта, через *переменная::метод*, и на конструктор, через *класс::new*. Лямбды также можно возврощать
## Встроенные функцианальные интерфейсы
* *Predicate\<T>* - *boolean test(T t)*
* *BinaryOperator\<T>* - *T apply(T t1, T t2)*
* *UnaryOperator\<T>* - *T apply(T t)*
* *Function\<T, R>* - *R apply(T t)*
* *Consumer\<T>* - *void accept(T t)*
* *Supplier\<T>* - *T get()*
## *Java.Util*
### Класс *Locale*
> Локали - боль... страдания... *C++*
*Locale.getDefault()* чтобы получить текущую
### *Date*
> *Date* - штука для работы с датами. Можно использовать для форматирования с помощью *Locale*
### *Calendar* & *GregorianCalendar*
> *Calendar* - абстрактный класс, а *GregorianCalendar* - имплементация. Он также держит часы, минуты, секунды. Класс *DateFormat* для красивого вывода
### *TimeZone* & *SimpleTimeZone*
> *Calendar* - абстрактный класс, а *GregorianCalendar* - имплементация. Он также держит часы, минуты, секунды. Нужны для работы с часовыми поясами
### *UUID*
> *UUID* - класс для работы с UUID
```Java
UUID id = UUID.randomUUID();
```
### *StringTokenizer*
> *StringTokenizer* нужен для разделения строк на токены, используя данный разделитель (по умолчанию пробел)