The interview time has passed for nearly a month. I have forgotten to sort it out. I sorted it out today.
I haven't had an interview for two months. I suddenly received such an interview notice. I wasn't fully prepared. In addition, I usually learned very widely and didn't go deep at all. If I asked a little deeper, I didn't understand. The most important thing is to clarify your thinking before answering. Don't answer what you think. This will make the interviewer sound confused and easy to make people feel yes rather than No. In this interview, I felt that my answer was yes rather than, which was very confusing. After almost 20 minutes of face-to-face, I felt basically cool. When I answered, I didn't organize the language well. I answered some yes rather than No. It's estimated that I was in a hurry. I didn't answer some questions, such as those related to thread pool. I didn't use much. When I learned, I just looked at the examples and finished. I didn't get what the interviewer was asking. I also rewritten the toString method (Rewriting the equals method) one by one. However, the interviewer is nice. I can hear from beginning to end, and I really wronged him.
If you realize your shortcomings, you should refuel!
I will summarize some questions in the interview as follows, which may be different from the actual questions.
Question 1: compare Java 8 and Java 11
Question 2: using Lambda to realize object de duplication, the essence of de duplication
For example, there is a Person class with age and name attributes. First, you need to rewrite the equals method and hashCode method.
package lambda; import java.util.Objects; /** * @author simon */ public class Person { private int age; public String name; public Person() { } public String getName() { return name; } public Person(int age, String name) { this.age = age; this.name = name; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Person person = (Person) o; return age == person.age && Objects.equals(name, person.name); } @Override public int hashCode() { return Objects.hash(age, name); } @Override public String toString() { return "Person{" + "age=" + age + ", name='" + name + '\'' + '}'; } }
Do not consider using Lambda for de duplication first
Method 1: use Set to remove duplicate
The elements in HashSet are unordered and the elements in TreeSet are ordered.
Using HashSet
public static void main(String[] args) { Person person1 = new Person(22, "ZhangSan"); Person person2 = new Person(22, "ZhangSan"); Person person3 = new Person(23, "LiSi"); Person person4 = new Person(24, "WangWu"); Person person5 = new Person(24, "WangWu"); Set<Person> set = new HashSet<>(); List<Person> personList = Arrays.asList(person1, person2, person3, person4, person5); set.addAll(personList); set.forEach(person -> System.out.println(person.toString())); }
It can be found that the output does not contain duplicate elements.
Using TreeSet
You need to override the Compare method of comparator.
public static void main(String[] args) { Person person1 = new Person(22, "ZhangSan"); Person person2 = new Person(22, "ZhangSan"); Person person3 = new Person(23, "LiSi"); Person person4 = new Person(24, "WangWu"); Person person5 = new Person(24, "WangWu"); Person person6 = new Person(25, "WangLiu"); List<Person> personList = Arrays.asList(person1, person2, person3, person4, person5, person6); Set<Person> set = new TreeSet<>((o1, o2) -> { if (o1.equals(o2)) { //return 0 means o1==o2 return 0; } else if (o1.getName().charAt(0) >= o2.getName().charAt(0)) { //return 1 means O1 > O2 return 1; } else { //return -1 means O1 < O2 return -1; } }); set.addAll(personList); set.forEach(person -> System.out.println(person.toString())); }
Output result: sorted by the first letter of name. If the initials are the same, the later ones are greater than the first ones by default. When sorting, put the small one in the front and the large one in the back.
Method 2: use sorting and then de duplication
public static void main(String[] args) { Person person1 = new Person(22, "ZhangSan"); Person person2 = new Person(22, "ZhangSan"); Person person3 = new Person(23, "LiSi"); Person person4 = new Person(24, "WangWu"); Person person5 = new Person(24, "WangWu"); Person person6 = new Person(25, "WangLiu"); List<Person> personList = Arrays.asList(person1, person2, person3, person4, person5, person6); Object[] objects = personList.toArray(); Person[] persons = new Person[objects.length]; for (int i = 0; i < objects.length; ++i) { persons[i] = (Person) objects[i]; } Arrays.sort(persons, ((o1, o2) -> { if (o1.equals(o2)) { return 0; } else if (o1.getName().charAt(0) >= o2.getName().charAt(0)) { return 1; } else { return -1; } })); System.out.println("------------Before weight removal------------"); for (Person person : persons) { System.out.println(person.toString()); } List<Person> uniquePerson = new ArrayList<>(); for (int j = 0; j < persons.length; ++j) { if (j < persons.length - 1 && persons[j].equals(persons[j + 1])) { continue; } else { uniquePerson.add(persons[j]); } } System.out.println("------------After weight removal------------"); uniquePerson.forEach(person -> System.out.println(person.toString())); }
Output:
Use Lamda again
Writing method I
public static void main(String[] args) { Person person1 = new Person(22, "ZhangSan"); Person person2 = new Person(22, "ZhangSan"); Person person3 = new Person(23, "LiSi"); Person person4 = new Person(24, "WangWu"); Person person5 = new Person(24, "WangWu"); Person person6 = new Person(25, "WangLiu"); List<Person> personList = Arrays.asList(person1, person2, person3, person4, person5, person6); Set<Person> set = new HashSet<>();//Use set to temporarily store non repeating elements //Use the stream method to publish elements one by one List<Person> uniquePerson = personList.stream().filter(person -> { if (!set.contains(person)) { set.add(person); return true; } else { return false; } }).collect(Collectors.toList()); uniquePerson.forEach(System.out::println); }
Writing method 2
public static void main(String[] args) { Person person1 = new Person(22, "ZhangSan"); Person person2 = new Person(22, "ZhangSan"); Person person3 = new Person(23, "LiSi"); Person person4 = new Person(24, "WangWu"); Person person5 = new Person(24, "WangWu"); Person person6 = new Person(25, "WangLiu"); List<Person> personList = Arrays.asList(person1, person2, person3, person4, person5, person6); List<Person> uniquePerson = personList.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new) ); uniquePerson.forEach(System.out::println); }
Output:
Question 3: tell me about the offline process pool
Refer to my article:
Creation and use of thread pool in Java. ThreadPoolExecutor and Executors
Question 4: why Spring Boot
At the beginning, Spring was born to eliminate the complexity of EJB, but Spring has become a behemoth today. There are dozens of modules in Spring framework, which makes it very difficult for developers to get started.
Spring Boot was born to simplify the development of spring, making the development simpler, the configuration simpler and the operation simpler.
Spring Boot is like a scaffold, which can provide us with basic configuration. For example, if we want to develop a simple web application, we only need to introduce the dependence of a web starter, and there is no need to make additional configuration.
Question 5: how to customize Spring Boot Starter
Refer to my article: