Week 2
Week 2
Interface Development: Auto-Complete Project-Related System Configuration Information
Draw a logic diagram of the interface
Code Review (SonarLint and CheckStyle)
Try to lower the cyclomatic complexity of the code
Code logic modification
Run a unit test for the API
Interface Development: Reminder Information Sending API
Learn a new CloudNative framework: ZA21 encapsulated inside CMB system
Check the source code of Working APP notification
Develop the APP and email notification interfaces
Create and run a unit test for the API
Interface Development: Remind Bank Manager Email Sending API
Develop the email notification interfaces
Create test data
Create and run a unit test for the API
Interface Development: Remind Project Finishing Email Sending API
Develop the email notification interfaces
Create test data
Create and run a unit test for the API
Interface Development: Database field modification
Check the source code of the project
Modify the database field
Create and run a unit test for the API
Interface Development: General Email Sending API
Develop the front end static pages in Vue.js
Technical Points:
1. computeIfAbsent
Overview
computeIfAbsent
is a method introduced in Java 8 as part of the Map
interface, offering a thread-safe way to check if a key exists in a map and, if not, compute and insert a new value for that key. This method helps avoid explicit conditional checks and potential race conditions, especially in multithreaded environments.
Method Signature
default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
key: The key whose associated value is to be mapped.
mappingFunction: A function that computes a value for the specified key if no mapping for the key exists or if the current mapping's value is null.
Return Value If the key is already associated with a non-null value, returns that value. Otherwise, invokes the provided function to compute a value, associates it with the key, and returns the computed value.
Example
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ComputeIfAbsentExample {
public static void main(String[] args) {
Map<Integer, String> map = new ConcurrentHashMap<>();
// Try to get the value for key 1, if absent, compute and put it
String result = map.computeIfAbsent(1, k -> "Value for " + k);
System.out.println(result); // Outputs: Value for 1
// Trying again should return the existing value without recomputing
String result2 = map.computeIfAbsent(1, k -> "This should not be called");
System.out.println(result.equals("Value for 1")); // Outputs: true
}
}
Map.Entry
Overview
Map.Entry<K,V> is an interface within the Java Collections Framework that represents a key-value pair within a Map. Each entry is associated with a unique key and a corresponding value. Using Map.Entry, you can conveniently iterate over a Map's key-value pairs without separately handling keys and values.
Main Methods
- K getKey(): Returns the key corresponding to this entry.
- V getValue(): Returns the value corresponding to this entry.
- V setValue(V value): Replaces the value corresponding to this entry with the specified value.
- boolean equals(Object o): Compares the specified object with this entry for equality.
- int hashCode(): Returns the hash code value for this entry based on its key and value.
Example
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapEntryExample {
public static void main(String[] args) {
// Create a HashMap instance
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 3);
map.put("Banana", 5);
map.put("Orange", 2);
// Get all entries (key-value pairs)
Set<Map.Entry<String, Integer>> entries = map.entrySet();
// Iterate over each entry in the Map
for (Map.Entry<String, Integer> entry : entries) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
// Modify the value
entry.setValue(entry.getValue() + 1);
}
// Print the updated Map
System.out.println("Updated Map: " + map);
}
}
3. keyset()
Overview The keySet()
method is a method of the Map
interface that returns a set view of the keys in the map. The set is backed by the map, so changes to the map are reflected in the set, and vice versa. The set supports element removal, which removes the corresponding entry from the map, via the Iterator.remove()
method.
Method Signature
Set<K> keySet()
- Return Value A set view of the keys in the map. The set is backed by the map, so changes to the map are reflected in the set, and vice versa. The set supports element removal, which removes the corresponding entry from the map, via the
Iterator.remove()
method.
Example
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class KeySetExample {
public static void main(String[] args) {
// Create a HashMap instance
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 3);
map.put("Banana", 5);
map.put("Orange", 2);
// Get the set of keys
Set<String> keys = map.keySet();
// Iterate over each key in the set
for (String key : keys) {
System.out.println("Key: " + key + ", Value: " + map.get(key));
// Remove the key-value pair from the map
map.remove(key);
}
// Print the updated Map
System.out.println("Updated Map: " + map);
}
}
4. Write MyBatis in XML
Don't forget use <
and >
to escape the <
and >
characters in XML.
5. in MyBatis
The test attribute in the <if>
tag is evaluated based on a Java expression from the passed parameters. If the first <if>
condition is true (i.e., projectType contains a specific value), MyBatis will not automatically combine the two conditions into the same WHERE clause, even if the second <if>
condition is also satisfied.
Additionally, the <where>
tag handles any extra AND and OR keywords, but it does not affect the logical order of the conditions. If you want to apply multiple conditions simultaneously in certain cases, you need to ensure that these conditions can coexist and are written correctly.
6. Difference between Logger Factory and log in lombok
The Logger Factory is a class provided by the slf4j library that allows you to create loggers for different classes. The log method is a static method provided by the Logger interface that allows you to log messages at different levels.
The log method in lombok is a shortcut for creating a logger and logging a message at the INFO level. It is a convenience method that is provided by the lombok library and is not part of the slf4j library.
7. @RequestBody and @ResponseBody in Spring MVC
The @RequestBody annotation is used to bind the request body to a method parameter. The @ResponseBody annotation is used to convert the return value of a method to a response body. If @RestController is used, @ResponseBody can be omitted.