On the 13th day, learn several knowledge points that are very helpful to programming, which can greatly reduce redundant operations in the programming process
1, lambda expression
lambad expression is an expression of the concept of functional programming. It can simplify many codes in programming.
1.1 several basic forms of lambda expressions
lambda expressions have parameters on the left, operations on the right, and - > connection in the middle.
left:
- No parameters: ()
- I. reference: o
- Multi parameter: (o1, o2,...)
right:
- o.col()
- o1.col()==?
- {expression;}
Where o - > o.col () can be simplified to class:: col
1.2 conditions of lambda expression
- An interface has one and only one abstract method.
- The interface has a @ FunctionalInterface tag
2, stream operation for collection
Create an employee class:
public class Employee { private int id; private String name; private int age; private String dept; public Employee(int id, String name, int age, String dept) { super(); this.id = id; this.name = name; this.age = age; this.dept = dept; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } @Override public String toString() { return id + "|" + name + "|" + age + "|" + dept; } }
Create a collection:
public static List<Employee> list = new ArrayList<Employee>(); static { list.add(new Employee(1, "Jack", 19, "DepartmentA")); list.add(new Employee(2, "Rose", 21, "DepartmentC")); list.add(new Employee(3, "Kitty", 23, "DepartmentA")); list.add(new Employee(4, "Tom", 34, "DepartmentB")); list.add(new Employee(5, "Lucy", 28, "DepartmentC")); }
2.1 single field projection
map() method
Name of everyone in the output set:
public static void main(String[] args) { list.stream().map(Employee::getName).forEach(System.out::println); }
Operation results:
Employee::getName
Here is a simplified formulation, equivalent to:
o -> o.getName()
2.2 screening
filter() method
Information of all persons over 22 years old in the output set:
public static void main(String[] args) { list.stream().filter(o -> o.getAge() > 22).forEach(System.out::println); }
Operation results:
2.3 judgment
noMatch() has no
anyMatch() exists
allMatch() all
Judge whether all the people in the collection are over 18 years old:
public static void main(String[] args) { boolean flag = list.stream().allMatch(o -> o.getAge() > 18); System.out.println(flag); }
Operation results:
2.4 paging
Skip
limit() interception
Intercept No. 2 to No. 4 information:
public static void main(String[] args) { list.stream().skip(1).limit(3).forEach(System.out::println); }
Operation results:
2.5 sorting
sorted() method
Sort the output of the collection by age:
public static void main(String[] args) { list.stream().sorted((o1, o2) -> { return o1.getAge() - o2.getAge(); }).forEach(System.out::println); }
Operation results:
2.6 collection
collect() method
Collect people younger than 24 years old:
public static void main(String[] args) { List<Employee> employeeList = list.stream().filter(o -> o.getAge() < 24).collect(Collectors.toList()); employeeList.forEach(System.out::println); }
Operation results:
Collect personnel information by department:
public static void main(String[] args) { Map<String, List<Employee>> employeeMap = list.stream().collect(Collectors.groupingBy(Employee::getDept)); employeeMap.entrySet().forEach(System.out::println); }
Operation results:
3, Thread
3.1 thread concept
A program contains one or more processes,
A process contains one or more threads,
A thread contains one or more coprocessors.
Generally, there is only one main thread for code running, which is usually called serial / blocking / synchronization,
Enabling multithreading allows different codes to execute at the same time. Multithreading is also known as parallel / non blocking / asynchronous.
3.2 implementation of multithreading
Methods to implement multithreading:
Inherit the Thread parent class or implement the Runnable interface and override the run() method.
Specific operations can be overridden by external classes or anonymous internal classes. Runnable interfaces can also be overridden by lambda expressions.
- Thread class:
// No. 0 Thread thread0 = new Thread() { @Override public void run() { for (int i = 0; i < 10; i++) { try { System.out.println(Thread.currentThread().getName() + ":This is the action of thread 0!" + i); Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } } }; // No. 1 Thread thread1 = new Thread() { @Override public void run() { for (int i = 0; i < 10; i++) { try { System.out.println(Thread.currentThread().getName() + ":This is the action of thread 1!" + i); Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } } }; // Start execution thread0.start(); thread1.start();
Operation results:
Thread.sleep(1000);
This sentence refers to system sleep. Fill in the time in parentheses. The unit is milliseconds.
Thread.currentThread().getName()
This sentence is used to display the name of the current thread. The thread name can be modified by adding a string in parentheses when creating an object.
thread0.start();
This sentence is used to start a thread.
2. Runnable interface
// No. 0 Runnable runnable0 = () -> { for (int i = 0; i < 10; i++) { try { System.out.println(Thread.currentThread().getName() + ":This is the action of thread 0!" + i); Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } }; // No. 1 Runnable runnable1 = () -> { for (int i = 0; i < 10; i++) { try { System.out.println(Thread.currentThread().getName() + ":This is the action of thread 1!" + i); Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } }; // Start execution new Thread(runnable0).start(); new Thread(runnable1).start(); }
Operation results:
new Thread(runnable0).start();
This sentence is used to start a Thread. To rewrite the Runnable interface, you need to create a Thread object to start it.