The temperature difference is great. I only want to go to work on warm days, but I'm thinking of accepting the bad routine of going to work on time because people don't have to be lazy by being tamed by habits.
Effective Java 3rd Edition "Stream abuse makes programs difficult to understand and maintain"
Suddenly, this is the conclusion. The short and easy-to-read code is great. However, the Stream API is also easy to generate short and esoteric code, long and esoteric code. When implemented by someone who doesn't fully understand the Stream API, including myself, I must always be aware of the risk of poor program readability. This is a problem for programmers in the field, and the Stream API is not bad. If you want to write the Stream API, change the location. Before the concise code from the Stream API you implemented spreads all over the place due to abuse by copy and paste and becomes a problem.
However, if it ends as it is, I'm sorry for some people who are still motivated to study Stream API, so I will introduce the code that makes Stream API more readable.
Now let's say you want to know which deals your employees are in charge of. How can I extract this information using a specific case list? As an implementation policy, 1, Acquisition of Record 2, Acquisition of the value to be treated as a key from Record 3, Create a map corresponding to the key value and Record Will be. The implementation by Stream API is as follows
/**
*Implementation by Stream API.
*It is just an explanation code and does not implement connection operation, exception handling, etc.
* @param salescodeList A list that stores code information that identifies the matter.
* @return <K, V> = {Employee code,List of projects in charge}
*/
public Map<Integer, List<Record>> getSalesListGroupingByEmployeeCode (List<Integer> salesCodeList) {
List<Record> recordList = searchBySalesCodeList(salesCodeList);
return recordList.stream().collect(
Collectors.groupingBy(recordList::getEmployeeCode));
}
First, I got a list of records. Then, make the list of acquired records into a Stream object. You can now work with the stream pipeline. Now let's sort out what we want to do with the stream pipeline. 1, I want to collect streams in a list 2, I want to perform grouping (mapping) using the employee code as a key So the implementation will be:
//Extracted the part using Stream API
recordList.stream(). //Convert List object to Stream object
collect( //Declare to collect stream objects as a list
Collectors.groupingBy( //Perform grouping
recordList::getEmployeeCode) //Pass the key value used for grouping as an argument
);
The StreamAPI focuses on "what to do", so such grouping and splitting would be a good example.
It is a change of direction when implemented by Stream API. If you disable the StreamAPI and return the same value as before, you'll probably have an implementation like this:
/**
*Implementation that does not use Stream API.
*It is just an explanation code and does not implement connection operation, exception handling, etc.
* @param salescodeList A list that stores code information that identifies the matter.
* @return <K, V> = {Employee code,List of projects in charge}
*/
public Map<Integer, List<SalesRecord>> getSalesListGroupingByEmployeeCode (List<Integer> salesCodeList) {
//HashMap to use as the return value of this method
Map<Integer, List<SalesRecord> result = new HashMap<>();
List<SalesRecord> recordList = searchBySalesCodeList(salesCodeList);
for (List<SalesRecord> record : recordList) {
int employeeCode = record.getEmployeeCode();
//Here, already Key-Determine if a Value pair has been generated
//If a pair has been created, the salesList can be obtained because the pair already exists in the result.
List<SalesRecord> salesList = result.get(employeeCode);
//If the pair does not exist, the list that stores the matter in charge cannot be stored, so a List is generated.
if (salesList == null) {
salesList = new ArrayList<>();
}
salesList.add(record);
result.put(employeeCode, salesList);
}
return result;
}
The number of lines has increased, and it's not very clear what you want to do.
The implementation example shown here was a better example to implement with Stream. However, there are some that should be implemented with a for statement instead of a Stream. If there are too many intermediate operations in the stream pipeline, some splitting may be necessary. After all, it's "which programmer prefers". It's not as clear as information hiding. If you're an enthusiastic supporter of the Stream API and you get tantrums when you're reviewing code that contains the Stream API and you're told "Stop the Stream API," you may not be there. In that case, actively adopt the Stream API and move around the site until you find an environment where you can maintain high code quality.
Recommended Posts