Java | Tests
1.82K subscribers
1 photo
1 link
Проверь свои знания в Java

✍️По всем вопросам: @Pascal4eg
Download Telegram
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class TestMutability {
public static void main(String[] args) {
Set<Employee> set = new HashSet<>();

Employee e1 = new Employee("Alice", 1000);
set.add(e1);

System.out.println(set.contains(e1));

e1.setSalary(2000);

System.out.println(set.contains(e1));
System.out.println(set.size());
}
}

class Employee {
private String name;
private int salary;

public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}

public void setSalary(int salary) {
this.salary = salary;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Employee)) return false;
Employee employee = (Employee) o;
return salary == employee.salary && Objects.equals(name, employee.name);
}

@Override
public int hashCode() {
return Objects.hash(name, salary);
}
}
❤1👍1
🎉1
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

public class Test {
public static void main(String[] args) {
Set<User> users = new TreeSet<>();

users.add(new User("Alice", 1000));
users.add(new User("Bob", 1000));
users.add(new User("Charlie", 1000));

System.out.println(users.size());
}
}

class User implements Comparable<User> {
private String name;
private int score;

public User(String name, int score) {
this.name = name;
this.score = score;
}

@Override
public int compareTo(User o) {
// сравниваем только по score
return Integer.compare(this.score, o.score);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
return score == user.score && Objects.equals(name, user.name);
}

@Override
public int hashCode() {
return Objects.hash(name, score);
}
}
❤1👍1
Что выведет код? 👆
Anonymous Quiz
54%
3
15%
2
18%
1
13%
<Ошибка компиляции>
🎉2
import java.lang.annotation.*;
import java.lang.reflect.Method;

public class Test {
public static void main(String[] args) throws Exception {
Method method = Service.class.getMethod("doWork");
if (method.isAnnotationPresent(Log.class)) {
System.out.println("Log annotation is present");
} else {
System.out.println("Log annotation is NOT present");
}
}
}

class Service {
@Log
public void doWork() {
System.out.println("Working...");
}
}

@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
@interface Log {
}
❤2👍2💅1
import java.lang.annotation.*;

public class Test {
public static void main(String[] args) {
printAnnotation(Child.class);
}

static void printAnnotation(Class<?> clazz) {
if (clazz.isAnnotationPresent(Marker.class)) {
System.out.println("Marker is present");
} else {
System.out.println("Marker is NOT present");
}
}
}

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Marker {
}

@Marker
class Parent {
}

class Child extends Parent {
}
👍3❤1
public class LambdaTest {
public static void main(String[] args) {
new LambdaTest().run();
}

public void run() {
Runnable r1 = new Runnable() {
@Override
public void run() {
System.out.println("Anonymous: " + this.getClass().getName());
}
};

Runnable r2 = () -> {
System.out.println("Lambda: " + this.getClass().getName());
};

r1.run();
r2.run();
}
}
❤2👍1
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 10;
int result = ++a + b++;
System.out.println(result);
}
}
👍3
Что выведет код? 👆
Anonymous Quiz
11%
15
61%
16
21%
17
8%
<Ошибка компиляции>
🎉3
import java.util.Random;

public class Test {
private static Random rnd = new Random();

public static void main(String[] args) {
StringBuffer word = null;
switch (rnd.nextInt(2)) {
case 1: word = new StringBuffer('P');
case 2: word = new StringBuffer('G');
default: word = new StringBuffer('M');
}
word.append('a');
word.append('i');
word.append('n');
System.out.println(word);
}
}
👍3
Что выведет код? 👆
Anonymous Quiz
10%
Pain
18%
Gain
29%
Main
13%
ain
31%
Pain или Gain
🤔7🎉3👍2